在数组中发送select的值

时间:2010-05-08 11:33:11

标签: php html ajax select arrays

我在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 />

我必须将已检查框的(1,3,...)发送到php脚本(我正在使用带有jquery的ajax),就像一个数组。类似于: drive_style [index]

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

在PHP脚本中:

print_r($_POST['drive_style']);
  

[object Object]


UPD : 我的新代码:

<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);
    });

警告空字符串。

4 个答案:

答案 0 :(得分:2)

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

这给你:

drive_style=1&drive_style=3&drive_style=5

但是对于使用php,您还需要输入如下名称:

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

注意: drive_style[]

显然给你了:

drive_style[]=1&drive_style[]=3&drive_style[]=5

答案 1 :(得分:1)

正如其他人所提到的,您应该根据典型的“数组”命名约定命名输入。 PHP会自动将请求变量中的数组语法转换为数组。因此,您应该以{{1​​}}形式命名您的复选框。要在JQuery中提交此信息,我们只需序列化表单:drive_style[name]应该做的伎俩。如下:

$('form_id').serialize()

在PHP端阅读这些信息也非常简单:

<!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>

值得注意的是,此命名约定还允许您定期提交表单。

答案 2 :(得分:0)

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

无论如何,你只能获得检查输入。

您可以使用

获取表单序列化数据
$('form_selector_here').serialize();

与POST和GET使用的格式相同(未检查的格式不在其中)

答案 3 :(得分:0)

在您使用输入数组的情况下,您必须为所有名称的输入添加[],如下所示:

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

之后,您可以在php代码中检索已检查的输入。

相关问题