PDA

Click to See Complete Forum and Search --> : Show hide textinput depending on checkbox?


wwwfilmfilercom
Nov 26th, 2006, 02:34 PM
Can php do this?

So I have some checkboxes on my form and if it is checked I want to let the user give a small description of it as well ie. show a textinput for them to be able to write in....

I'm thinking either when the checkbox is true it could somehow dynamically create a textinput (hmmm not sure if I've seen this...)

OR

I could have disabled textinputs next to the checkboxes and clicking on a checkbox will enable the textinput next to it?

Are there any [better] methods and if not which should I go for and how? Thx

kows
Nov 26th, 2006, 03:51 PM
you need to use Javascript to do this. php is a server-side language and only outputs HTML, after the website is served to the client, PHP cannot physically do anything further to that website. Javascript is client-side and can be used to manipulate a website after it has been served to the client, which is exactly what you're looking for. A quick search on Google for something like 'disabling HTML with Javascript' should give you plenty of examples. If not, you can always check out Dynamic Drive (http://www.dynamicdrive.com), which has a large collection of Javascript-scripts, and I'm sure it also has what you're looking for, among many other things.

The Hobo
Nov 26th, 2006, 04:27 PM
Of course you're probably going to want to disable/enable them onload depending on the default state of the checkbox, but:

<form action="blah.php" method="post">
<input type="checkbox" name="checkbox"
onclick="this.form.inputtext.disabled = !this.checked" />
<input type="text" name="inputtext" value="" />
</form>

wwwfilmfilercom
Nov 26th, 2006, 04:45 PM
Hmmm I'm in a muddle with this - I want to add javascript and I could do this on a normal html page but my pages are structured like this:


<?php
require( "header.php" );
?>

body

<?php
require( "footer.php" );
?>


So how can I add the relevant js code and still have it relevant to individual pages?

The Hobo
Nov 26th, 2006, 04:55 PM
You would add it wherever you are generating the check boxes and and text inputs? I'm not sure what you're confusion is. How are you generating these check boxes?

wwwfilmfilercom
Nov 26th, 2006, 05:00 PM
I'm doing things like this:


echo "<input name='skills[]' type='checkbox' value='$skillid'>$skillname: <textarea rows='3' cols='20' name=txt$skillname></textarea><br />";

$i++;
}


So Im generating them using the echo function.

For some JS (not necessarily disable/enable) you have to include something in the header folder - but I'm thinking that whatever I include in the header will display on all pages cos I'm using header with the require function... I'm thinking that I don't want it to conflict with other JS code I might need or with forms on other pages... I'm actualy getting prety confused over something which is probably quite simple...lol

(btw I like the doug pic)

The Hobo
Nov 26th, 2006, 05:04 PM
For the simple code that I provided above, you do not need to include anything in the header. You do not need any <script> tags anywhere. You just put it in the onclick outright.

As for enabling/disabling on startup, I would just do that with PHP. If you want to disable something to begin with (ie, the check box is not checked by default), add disabled="disabled" as an attribute for the textarea.

The Hobo
Nov 26th, 2006, 05:05 PM
I would not worry about your javascript in headers showing up on every single page (even if it is not needed on that page) unless you have MASSIVE javascript routines.

And you shouldn't have to worry about conflicting with other javascript code if you just make sure to give each function a unique name, ie: "ToggleTextArea( bEnabled )".

This will only conflict with something else if you give it the same name (declare it twice).

wwwfilmfilercom
Nov 26th, 2006, 05:16 PM
Got ya ;)

Hey I tried what you advised above - so basically now wat I have done is set each checkbox to CHECKED on page load and when the user unchecks a box the corresponding textinput gets disabled.

Works fine for me! Thx ;)

Also I note what you say about JS in PHP with the headers and require etc.. Ill test it out more later, guess I was worrying about nothing (as usual lol)