• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

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
0
dm14011
Asked:
dm14011
  • 4
  • 3
  • 2
2 Solutions
 
Diablo84Commented:
It's difficult to be specific without knowing how your code works, however...

Checkbox data will only exist in the POST array if they are set (ie. they have been checked). If a checkbox is not checked when the form is submitted, it's key and value will not be present in the post array.

For example:

<input type="checkbox" name="mycheck" value="myvalue">

And the PHP:

<?php
if (array_key_exists('mycheck',$_POST)) {
 echo "the checkbox was checked, value: {$_POST['mycheck']}";
}
else {
 echo "the checkbox was not checked";
}
?>

Diablo84
0
 
dm14011Author Commented:
so no actual data is sent? Either $_POST['mycheck'] exists or not?
0
 
AlanJDMCommented:
Just a thought...

When you create the checkboxes are you including a value property? When you are using text boxes the value comes from data entered by the user. When using checkboxes you are resposible for putting ther value you want returned in the value property of the input tag.


Alan
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
Diablo84Commented:
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
0
 
Diablo84Commented:
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

0
 
AlanJDMCommented:
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
0
 
Diablo84Commented:
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
0
 
AlanJDMCommented:
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
0
 
dm14011Author Commented:
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
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now