繁体   English   中英

在数组中发送选择的值

[英]Send select's values in array

我在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脚本(我将ajax与jquery一起使用)像数组一样。 类似于: drive_style [index]

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

在PHP脚本中:

print_r($_POST['drive_style']);

[对象对象]


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

它警告空字符串。

​$(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

正如其他人提到的那样,您应该根据典型的“数组”命名约定来命名输入。 PHP将自动将您的请求变量中的数组语法转换为数组。 因此,您应该以drive_style[name]的形式命名复选框。 要在JQuery中提交此信息,我们只需序列化表单即可: $('form_id').serialize()应该可以解决问题。 如下:

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

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

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

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

无论如何,您只会得到选中的输入。

您可以使用以下方式获取表格序列化数据

$('form_selector_here').serialize();

这与POST和GET所使用的格式相同(并且未经检查的格式将不在其中)

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

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

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

暂无
暂无

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

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