Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Serializing checkboxes for PHP

    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:

    Code:
    <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:
    Code:
    $("#previewBtn").click(function(e){
                    e.preventDefault();
                    $(this).hide();
                    $("#prevStepBtn").hide();
    
                    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');  
                                    $(".multiform_part").html(data);
                    });
    Here is the output from PHP print_r, after I have selected all options:

    Code:
    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:

    Code:
    health%5B%5D=asthma&health%5B%5D=alzheimer&health%5B%5D=arthritis&fm-meds=&fm-acc=&fm-allergies=&fm-diseases=&fm-injurydetails=&fm-alldetails=
    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.

    Thanks.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    The serialization looks correct, to me. I would suspect the error is in your PHP code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is the PHP code, am I accessing the values correctly?

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

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Quote Originally Posted by roonui View Post
    This is the PHP code, am I accessing the values correctly?

    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    $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.

  • #6
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, I think I found the issue:

    Code:
    session_start();
    
    $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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    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. ??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •