PHP - ALL checkbox data sent with POST

  • rkravarik
  • Born
  • Born
  • rkravarik
  • Posts: 3

Post 3+ Months Ago

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!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • AnarchY SI
  • Web Master
  • Web Master
  • User avatar
  • Posts: 2521
  • Loc: /usr/src/MI

Post 3+ Months Ago

check out this post and this post and see if they help..
  • rkravarik
  • Born
  • Born
  • rkravarik
  • Posts: 3

Post 3+ Months Ago

I figured it out. It was actually a js function (apparently incomplete) that wasn't verifying that the boxes were checked before sending the values. It had noting to do with PHP. Go figure. Thanks for the reply AnarchY.
  • AnarchY SI
  • Web Master
  • Web Master
  • User avatar
  • Posts: 2521
  • Loc: /usr/src/MI

Post 3+ Months Ago

np, glad you got it figured out :]
  • goodamr
  • Newbie
  • Newbie
  • goodamr
  • Posts: 5
  • Loc: http://elearningking.blogspot.com

Post 3+ Months Ago

Thanks alot for this great tutorial.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 78 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.