arbitter Posted April 14, 2010 Share Posted April 14, 2010 Hello there. I'd like a form with one 'checkbox' type button. If that button is not selected, another part of the form, containing other select buttons and a text field, should be disabled. When you select the first button, the rest is also activated. After that, there is also a file type as input. (Or should I split it into multiple forms?) I'll give a example of the form: <?php echo "<form action='$_SERVER[php_SELF]' method='post' enctype='multipart/form-data'> <br /> the checkbox that determines if the others are active:<input type='checkbox' name='folder' value='yes' /> <br /> <input type='radio' name='name' /><input type='text' length='20' name='name' /><br /> <input type='radio' name='name' value='folderone' /> <br /> <input type='radio' name='name' value='foldertwo' /> <br /> The file:<input type='file' name='image' /> <br /> <input type='submit' name='submit' value='submit' />"?> So in this example, the three radio types should be grey, not clickable. Ofcourse you should be able to check and uncheck the first box... Quote Link to comment Share on other sites More sharing options...
arbitter Posted April 14, 2010 Author Share Posted April 14, 2010 I seem to have found a site with decent explanations, I'll ask further questions later... Quote Link to comment Share on other sites More sharing options...
arbitter Posted April 14, 2010 Author Share Posted April 14, 2010 Well, it still doesn't work... This is what I have: <html> <head> <script type='text/javascript'> function start() { myform.name.disabled = true; } onload = start; function chgtx() { myform.name.disabled = !myform.cb.checked; } </script> </head> <body> <?php echo " <form action='$_SERVER[php_SELF]' method='post' enctype='multipart/form-data' name='myform'> <br /> the checkbox that determines if the others are active:<input type='checkbox' name='cb onchange='chgtx();' /> <br /> <input type='radio' name='name' /><input type='text' length='20' name='name' /><br /> <input type='radio' name='name' value='folderone' /> <br /> <input type='radio' name='name' value='foldertwo' /> <br /> The file:<input type='file' name='image' /> <br /> <input type='submit' name='submit' value='submit' />"; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 14, 2010 Share Posted April 14, 2010 Lot's of problems: 1. There is at least one error in your HTML: Missing the closing quote mark for the name of the checkbox. 2. You gave the fields a name of, well, "name". This is bad because you try to reference the fields via "myform.name.disabled", where "name" is the name of the fields. But, the form has a name property (i.e. "myform") so there is a conflict. 3. a radio button group is multiple elements. you can't enable/disable multiple elements with a single line like that. 4. onchange doesn't work for a checkbox until you exit the field, you want to use onclick <html> <head> <script type='text/javascript'> function enableRadioGroup(groupName, enableState) { var groupObj = document.forms['myform'].elements[groupName]; var groupLength = groupObj.length; for (var i=0; i<groupLength; i++) { groupObj[i].disabled = (!enableState); } enableTextBox(groupObj[0].checked); } function enableTextBox(enableState) { document.forms['myform'].elements['textName'].disabled = (!enableState); } function start() { enableRadioGroup('radioName', false); } onload = start; </script> </head> <body> <form action="" method="post" enctype="multipart/form-data" name="myform"> <br /> the checkbox that determines if the others are active: <input type="checkbox" name="cb" onclick="enableRadioGroup('radioName', this.checked);" /> <br /><br /> <input type="radio" name="radioName" onclick="enableTextBox(this.checked);" /> <input type="text" length="20" name="textName" /><br /> <input type="radio" name="radioName" value="folderone" onclick="enableTextBox(!this.checked);" /><br /> <input type="radio" name="radioName" value="foldertwo" onclick="enableTextBox(!this.checked);" /><br /> <br /> The file:<input type="file" name="image" /> <br /> <input type="submit" name="submit" value="submit" /> </body> </html> Quote Link to comment Share on other sites More sharing options...
arbitter Posted April 15, 2010 Author Share Posted April 15, 2010 Thanks a lot, mjdamato! I had found another metod to make the things deselected; but after configuring a code for more than an hour I noticed it only worked on one single radio! It works perfectly now, even the radio with the textbox needs to be activated for the textbox to be active. I'll study through your code for a while to make sure I fully understand. You did in 5 minutes what I couldn't do in a whole night Thank you! Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 15, 2010 Share Posted April 15, 2010 You did in 5 minutes what I couldn't do in a whole night If you were to ask my wife, that probably isn't a good thing. Quote Link to comment Share on other sites More sharing options...
arbitter Posted April 15, 2010 Author Share Posted April 15, 2010 You did in 5 minutes what I couldn't do in a whole night If you were to ask my wife, that probably isn't a good thing. And you have an incredible sense of humor Quote Link to comment Share on other sites More sharing options...
arbitter Posted April 16, 2010 Author Share Posted April 16, 2010 Okay I have another problem though... It is possible that there are no other radio-options except for the one with the text. Then, the script does not work. How do I fix that? (I check the database for if a user might have made any folders. If so, those folders are displayed in the radio's. The user can also always make a new folder.(this all works, except some minor mySQL problems)) Quote Link to comment Share on other sites More sharing options...
arbitter Posted April 16, 2010 Author Share Posted April 16, 2010 Used the code of my second comment, works like a charm! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.