View Full Version : Serializing checkboxes for PHP

07-07-2012, 12:24 AM
Hello, I am having a problem where I am trying to send checked checkbox data to a PHP page for processing, but the PHP is only picking up on the first checked option. Here is a sample of the HTML:

<input style="width:20px;" type="checkbox" name="health[]" value="asthma" />Asthma
<input style="width:20px;" type="checkbox" name="health[]" value="alzheimer" />Alzheimers
<input style="width:20px;" type="checkbox" name="health[]" value="arthritis" />Arthritis

Here is the JS function that serializes the result, basically it is a multi page from that utilises Javascript to serialise all the pages page data into one request to PHP instead of multiple PHP requests, so its just getting the current form, then adding the data to the string:


var curr = $(".stepSelected").children(':eq(1)').children().attr('id');
var formData = $("#form_"+curr).serialize();

$.get('processForm.php', {saveData: curr, formData: formData, step : 'getAllData'}, function(data) {
$("#stepTable tr").removeClass('stepSelected');

Here is the output from PHP print_r, after I have selected all options:

print_r =
Array ( [health] => arthritis[fm-meds] => xcxcxc [fm-acc] => [fm-allergies] => [fm-diseases] => [fm-injurydetails] => [fm-alldetails] => )

And here is the serialized string from JS:


I have become pretty certain that it is the JS that is causing the issue, have been hacking away but just cant seem to get all the checked results into PHP. All text inputs work fine, and I have not tried with radio buttons yet, but they are next. Any help much appreciated.


Old Pedant
07-07-2012, 01:32 AM
The serialization looks correct, to me. I would suspect the error is in your PHP code.

07-10-2012, 02:35 AM
This is the PHP code, am I accessing the values correctly?

foreach ($_SESSION['formstep4'] as $key => $value) {
if ($key == "health"){
$msg .= "Form Step 4: ".$key." == ".$value."<br>\n";

Old Pedant
07-10-2012, 04:43 AM
This is the PHP code, am I accessing the values correctly?

foreach ($_SESSION['formstep4'] as $key => $value) {
if ($key == "health"){
$msg .= "Form Step 4: ".$key." == ".$value."<br>\n";

Nope, not even close. That code has NOTHING to do with extracting form field values from a post. That is looking at *SESSION* values.

You need code that is looking at $_POST[] or $_GET[] values (depending on what method you used in the JS code...you are using jQuery, I assume, and it *looks* like you are using get but I don't use jQuery so not sure).

Time to post in the PHP forum, methinks.

07-10-2012, 05:36 AM
Hmm ok, guess I should note that this is a 3rd party script I am modifying (multiform). They use session in the original code and it all works fine, until I go to add checkboxes and radio buttons, eg:

$msg .= "Phone: ".$_SESSION['formstep3']['fm-ref1phone']."<br>\n<br>\n";
$msg .= "Reference name: ".$_SESSION['formstep3']['fm-ref2name']."<br>\n";
$msg .= "Company: ".$_SESSION['formstep3']['fm-ref2comp']."<br>\n";
$msg .= "Position: ".$_SESSION['formstep3']['fm-ref2pos']."<br>\n";
$msg .= "Email: ".$_SESSION['formstep3']['fm-ref2email']."<br>\n";

are all standard text inputs, and work fine. But yes, will move to the PHP section.

07-10-2012, 05:40 AM
Actually, I think I found the issue:


$step = $_REQUEST['step'];
$formData = $_REQUEST['formData'];

function save($step, $data) {
//$_SESSION[$step] = $data;
$fields = explode('&',$data);
foreach ($fields as $field) {
$keyVal = explode('=', $field);
$key = urldecode($keyVal[0]);
$val = urldecode($keyVal[1]);
$_SESSION[$step][$key] = $val;

if ($formData) {
$formStep = $_REQUEST['saveData'];
save($formStep, $_REQUEST['formData']);

This code is not taking into consideration the possibility for checkbox arrays.

Old Pedant
07-10-2012, 07:55 PM
Right you are. And fixing it might cause more problems other places.

How about abandoning the multiple checkboxes with same names and just naming them "health1", "health2", "health3", etc. ??

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum