Forum Moderators: coopster

Message Too Old, No Replies

ALL checkbox data posting regardless of check state

PHP checkbox processing

         

konductive

8:56 pm on Nov 21, 2008 (gmt 0)

10+ Year Member



Hello,

I seem to be having a problem with some code I have written to collect data from checkboxes in an HTML form, and pass them to database values. I will start by giving a short example of my form data:

<form action="form.php" method="post">
<input type="checkbox" name="ckbx[]" value="val1" />Value 1<br />
<input type="checkbox" name="ckbx[]" value="val2" />Value 2<br />
<input type="checkbox" name="ckbx[]" value="val3" />Value 3<br />
etc...
<input type="button" value="Submit" onclick="submitForm(); return false;" /> <--- The return false here is because I am submitting this form via AJAX

I have found that when I submit the form, the 'ckbx' array is being passed ALL of the checkbox values, not just the ones that are checked. For example, I have this code in my form.php file for debugging:

$checkboxes = $_POST['ckbx'];
$string = implode($checkboxes,",");
$msg = $string;
$msg2 = $_POST['ckbx'];
...
echo $msg;
print_r($msg2);

...and regardless of which are checked or unchecked I get this output:

val1, val2, val3 Array ( [0] => val1 [1] => val2 [2] => val3 )

Now, by doing heavy research on the POST values of checkboxes, no values should be sent for boxes that weren't checked. But for some reason, they are all set to 'on' every time, even if they are all unchecked. Any help would be greatly appreciated.

BTW, this is my first post. If I have posted this in the wrong area, please let me know. Thanks!

cameraman

11:05 pm on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World, konductive.

Just for grins can you temporarily submit that form "straight" - without any help from javascript? Before I even read about the ajax my first thought was that it almost seemed like javascript might be doing something to it.

konductive

4:31 am on Nov 22, 2008 (gmt 0)

10+ Year Member



You were right!

I spent SEVERAL hours looking at the form and php script, trying to see what I was doing wrong. I wasn't even thinking about the for loop in my js script that was collecting the form info. 1 if statement later and everything is working like a charm. Thank you very much!