Jump to content

disable a form


arbitter

Recommended Posts

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' />"?>

4ggfbt.jpg

 

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...

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 :P

Thank you!

Link to comment
Share on other sites

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))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.