0

I have such select list in html:

<input type="checkbox" name="drive_style" value=1 checked />123<br />
<input type="checkbox" name="drive_style" value=2 />123<br />
<input type="checkbox" name="drive_style" value=3 checked /> 123<br />
<input type="checkbox" name="drive_style" value=4 />123<br />
<input type="checkbox" name="drive_style" value=5 checked />123<br />

I have to send values(1, 3, ...) of checked boxes to the php script (I'm using ajax with jquery) like an array. Something like: drive_style[index].

    $.post('post_reply.php', {'drive_style'  : $('drive_style')}, function(data){
        alert(data);
    });

In PHP script:

print_r($_POST['drive_style']);

[object Object]


upd: My new code:

<input type="checkbox" name="drive_style[]" value=1 checked />123<br />
<input type="checkbox" name="drive_style[]" value=2 />123<br />
<input type="checkbox" name="drive_style[]" value=3 checked />123<br />
<input type="checkbox" name="drive_style[]" value=4 />123<br />
<input type="checkbox" name="drive_style[]" value=5 checked />123<br />

    $.post('post_reply.php', {'drive_style':$('input[name=drive_style]').serialize()}, function(data){
        alert(data);
    });

It alerts empty string.

1
  • i have mentioned input[name=drive_style] must be input[name=drive_style[]] Commented May 8, 2010 at 12:01

4 Answers 4

2
​$(function() {
var serial = $('input[name=drive_style]').serialize();
//php -> $('input[name=drive_style[]]').serialize();
  alert( serial );
});​

this give you:

drive_style=1&drive_style=3&drive_style=5

but for work with php you also need to have input name like this:

<input type="checkbox" name="drive_style[]" value=1 checked />123<br />

NOTE: drive_style[]

that obviously giv you:

drive_style[]=1&drive_style[]=3&drive_style[]=5
Sign up to request clarification or add additional context in comments.

3 Comments

Hm, serialize gives me something like drive_style=1&... But after adding '[]' to the input's name it's empty in result.
read my post well //php -> $('input[name=drive_style[]]').serialize();
well, with: $('input[name=drive_style[]]').serialize() as post-data I have the same empty string.
1

As others have mentioned, you should name your inputs according to the typical 'array' naming convention. PHP will automatically turn array syntax in your request variables into an array. Thus, you should name your checkboxes in the form drive_style[name]. To submit this information in JQuery we simply serialize the form: $('form_id').serialize() ought to do the trick. As follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Test</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" charset="utf-8">

            function jquerySubmit()
            {
                $.post('http://localhost/test.php', $('#checkboxform').serialize(), function(data) {
                    alert(data);
                });
            }

        </script>
    </head>
    <body>
        <form id="checkboxform" action="http://localhost/test.php" method="post">

            <input type="checkbox" name="drive_style[one]" /> One<br />
            <input type="checkbox" name="drive_style[two]" /> Two<br />
            <input type="checkbox" name="drive_style[three]" /> Three<br />

            <input type="submit" value="Submit Form" /> <input type="button" value="Submit AJAX Request" onclick="jquerySubmit()" />
        </form>
    </body>
</html>

And to read this information on the PHP side is also very simple:

// http://localhost/test.php
<?php

    echo time(), "\n";

    if (isset($_POST['drive_style']) && is_array($_POST['drive_style']))
    {
        echo implode(", ", array_keys($_POST['drive_style']));
        echo "\n\n";    
    }

    print_r($_POST);

Notably this naming convention also allows you to submit the form regularly.

Comments

0
<input type="checkbox" name="drive_style[key]" value=2 />123<br />

You'll get only checked inputs anyways.

You can get forms serialized data with

$('form_selector_here').serialize();

which is the same format used for POST and GET ( and not-checked ones won't be in there )

1 Comment

It may be handy to just omit the key portion and use drive_style[], unless you need to assign the checkboxes to specific keys.
0

In your case you are using an array of input, you must add [] for all your name's input like this:

<input type="checkbox" name="drive_style[]" value=1 checked />123<br />
<input type="checkbox" name="drive_style[]" value=2 />123<br />
...

after that you can retrieve the checked input in your php code.

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.