Link to home
Start Free TrialLog in
Avatar of dm14011
dm14011

asked on

checkbox not posting

Hi,
Using php and posting I am submitting data from forms with posts. All my forms are created dynamically. I have a working form which submits and a updates values from text boxes. Now however I have changed them to checkboxes. When the boxes are posted there is no data in the posted fields where checkboxes are concerend. The only change from the working model is that I have changed some of the text fields to checkboxes. Does anyone know or have any idea why there is no data posted in them like true or false?
thanks
SOLUTION
Avatar of Diablo84
Diablo84

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dm14011
dm14011

ASKER

so no actual data is sent? Either $_POST['mycheck'] exists or not?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If the checkbox is not checked then the key 'mycheck' will not exist in the POST array, so $_POST['mycheck'] will not exist.

It will only be in the post array if it is checked when the form is submitted.

Diablo84
You can see exactly what happens here:

<pre>
<?php
if (array_key_exists('submit',$_POST)) print_r($_POST);
?>
</pre>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="test" value="example">
<input type="checkbox" name="mycheck" value="myvalue">
<input type="submit" name="submit" value="Submit">
</form>

If it's checked the key and corresponding value of the checkbox will be in the post array, if it is not checked they will not be present.

Diablo84

I realize that. But when I read this...

"why there is no data posted in them like true or false"

My thought is that the poster is looking for 'values' of checked checkboxes that may not be there because he isn't assigning any values.


Alan
Sorry Alan,

my reply was to dm14011, i hadn't noticed your comment above.

>> because he isn't assigning any values

You may well be right there. It's unusual for checkboxes to be used for sending data, it's usually if they are checked do this, else do that... now you mention that i am wondering if it might be more suitable to use hidden fields here. It depends on how the script is intended to function.

Diablo84
As you originally stated, we really dont have enough info to properly answer this post. I was only thinking that because he...

"changed them to checkboxes"

He may have forgotten to add values he was expecting to see. It is more likely the scenario you described. I was just adding something else to look at.


Alan
Avatar of dm14011

ASKER

Ah yes, the value setting. Good point. I got confused with this before as I expected that a value of yes would cause the check box to become 'pre'-checked. However one must add the word CHECKED to do this as in <INPUT type = 'checkboxed' name = 'aname' CHECKED value = 'avalue'> and then on submission avalue will be posted in $_POST['aname'] if and only if the check box is checked.
thanks for the help