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.

5
  • 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. Commented Jan 6, 2012 at 8:11
  • He is not sending the data as JSON. The dataType parameter indicates the server response content type, not the request. Commented Jan 6, 2012 at 8: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 Commented Jan 6, 2012 at 8: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' }). Commented Jan 6, 2012 at 8:19
  • @Darin I'm going to stop while, I'm (somewhat) ahead. Still, I suppose this is the best way to learn Commented Jan 6, 2012 at 8:21

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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()

Comments

-2

there are an Error in your code :

  1. The url should be url: 'http://localhost/test/generate.php' with the extension name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.