2

JQUERY:

$(document).ready(function(){
    $('form').submit(function(){
        var content = $(this).serialize();
        //alert(content);
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: 'http://localhost/test/generate',
            timeout: 15000,
            data:{  content: content },
            success: function(data){
                $('.box').html(data).fadeIn(1000);
            },
            error: function(){
                $('.box').html('error').fadeIn(1000);
            }
        });
        return false;
    });
});

HTML:

<form>
<input type="checkbox" value="first" name="opts[]">
<input type="checkbox" value="second" name="opts[]">
<input type="checkbox" value="third" name="opts[]">
<input type="submit">
</form>

How do i process (or read) multiple checked checkbox's value in PHP? I tried doing $_POST['content'] to grab the serialized data but no luck.

Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133
  • 1
    Is there a reason you need to send the data as JSON? A default post of the form will make your data available as `$_POST['opts']` (it will return an array). If you need to keep it as json, try `var_dump($_POST)` to see what you're getting. – Grexis Jan 06 '12 at 08:11
  • He is not sending the data as JSON. The `dataType` parameter indicates the server response content type, not the request. – Darin Dimitrov Jan 06 '12 at 08:13
  • @Darin Ah, I haven't worked with jQuery much, (Does it show? :D) but I was referring to his serialization of the form and the `data:{ content: content }` line – Grexis Jan 06 '12 at 08:17
  • @Grexis, this is not JSON. jQuery automatically serializes this javascript literal into an `application/x-www-form-urlencoded` request. Nothing to do with JSON. If you wanted to send JSON to the server you would specify the `contentType: 'json'` and you would JSON serialize the request: `data: JSON.stringify({ content: 'some content' })`. – Darin Dimitrov Jan 06 '12 at 08:19
  • @Darin I'm going to stop while, I'm (somewhat) ahead. Still, I suppose this is the best way to learn – Grexis Jan 06 '12 at 08:21

4 Answers4

5

Replace:

data:{  content: content } // <!-- you are prefixing with content which is wrong

with:

data: content

Now in your PHP script you can use $_POST['opts'] which normally should return an array.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

Try

echo $_POST['opts'][0]. "<br />";
echo $_POST['opts'][1]. "<br />";
echo $_POST['opts'][2]. "<br />";

You post an array to the Server and it is available in the post variable 'opts'. Remember: Unchecked boxes dont get posted.

Grrbrr404
  • 1,809
  • 15
  • 17
0

The chosen answer still didn't work for me, but here is what did:

var checkedBoxesArr = new Array();
$("input[name='checkedBoxes']:checked").each(function() {
    checkedBoxesArr.push($(this).val());
});
var checkedBoxesStr = checkedBoxesArr.toString(); 
var dataString = $("#" + formID).serialize() + 
                  '&checkedBoxesStr=' + checkedBoxesStr;

[The above code goes in your javascript, before serializing the form data]
First, cycle through the checked boxes and put them into an array.
Next, convert the array to a string.
Last, append them to the serialized form data manually - this way you can reference the string in your PHP alongside the rest of the serialized data.

This answer came partly from this post: Send multiple checkbox data to PHP via jQuery ajax()

Community
  • 1
  • 1
Mark Pruce
  • 945
  • 10
  • 25
-2

there are an Error in your code :

  1. The url should be url: 'http://localhost/test/generate.php' with the extension name
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65