简体   繁体   English

在数组中发送选择的值

[英]Send select's values in array

I have such select list in html: 我在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. 我必须将复选框的 (1、3,...)发送到php脚本(我将ajax与jquery一起使用)像数组一样。 Something like: drive_style[index] . 类似于: drive_style [index]

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

In PHP script: 在PHP脚本中:

print_r($_POST['drive_style']);

[object Object] [对象对象]


upd : My new code: 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);
    });

It alerts empty string. 它警告空字符串。

​$(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: 但要使用php,您还需要输入如下名称:

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

NOTE: drive_style[] 注意: drive_style[]

that obviously giv you: 那显然给了你:

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

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. PHP将自动将您的请求变量中的数组语法转换为数组。 Thus, you should name your checkboxes in the form drive_style[name] . 因此,您应该以drive_style[name]的形式命名复选框。 To submit this information in JQuery we simply serialize the form: $('form_id').serialize() ought to do the trick. 要在JQuery中提交此信息,我们只需序列化表单即可: $('form_id').serialize()应该可以解决问题。 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: 在PHP方面阅读此信息也非常简单:

// 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. 值得注意的是,该命名约定还允许您定期提交表单。

<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 ) 这与POST和GET所使用的格式相同(并且未经检查的格式将不在其中)

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. 之后,您可以在php代码中检索选中的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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