简体   繁体   中英

Extracting JQuery POST parameters in PHP

I am trying to pass a group of checkboxes to PHP using JQuery. I am using the code from Passing multiple checkbox items to PHP file using JSON and querying a DB Should I post this under that question?

The checkboxes are one for each day in the month in a calendar. Values are integer days of the month

<td><input name="check[]" value="4" type="checkbox">4</td>
<td><input checked="yes" name="check[]" value="7" type="checkbox">7</td>

The checkboxes are within a table in a form.

<form id="checkboxes">
<table id="calendarId" class="calendar"> 

I use Jquery to pass the checkboxes to PHP:

   $("#calendarId").load($.post("calendar.php",{check: $("form#checkboxes").serialize()}));

Firebug shows that a list of checked boxes is in the post parameters. The two days I checked to generate this example were 7 and 21 which are discernible in the code.

check   check%5B%5D=7&check%5B%5D=21
Source
check=check%255B%255D%3D7%26check%255B%255D%3D21

My problem is that I am unable to extract these values in PHP.

parse_str($_POST['check'], $checkboxes); 

returns an empty string, as does looping over the POST array:

foreach($_POST as $key=>$val){
    $test .= $key . "; " . $value . "; ";
}

Trying to loop over $_POST['check'] generates an error which I assume means that $_POST['check'] is empty.

The complete page is at http://www.genomics.liv.ac.uk/shereMuseum/calendar1.html

I have also tried to pack the checkboxes into a data structure "checkboxes" before posting:

        var checkboxes = new Array();
        $("input[name='check[]']:checked").each(function(){checkboxes.push($(this).val());});

or

        $.each($("input[name='check[]']:checked"),function() {
                checkboxes.push($(this).val());
        });

But then Firebug reports the POST parameters as:

undefined   undefined
undefined   undefined
Source
undefined=undefined&undefined=undefined

I would be very grateful for any suggestions. Harry

Not sure why you are using $('#calendarId').load(...), but have you tried doing it like

$.ajax({
    type: 'POST',
    url: 'calendar.php',
    data: $('#checkboxes').serialize(),
    success: function(response) {
        // handle the response here
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM