【问题标题】:Send select's values in array在数组中发送选择的值
【发布时间】:2011-02-17 03:27:32
【问题描述】:

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

我必须像数组一样将 checked 框的 values(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']);

[对象对象]


更新: 我的新代码:

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

它警告空字符串。

【问题讨论】:

  • 我已经提到input[name=drive_style] 必须是input[name=drive_style[]]

标签: php html ajax select arrays


【解决方案1】:
<input type="checkbox" name="drive_style[key]" value=2 />123<br />

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

您可以通过

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

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

【讨论】:

  • 省略key 部分并使用drive_style[] 可能会很方便,除非您需要将复选框分配给特定键。
【解决方案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

【讨论】:

  • 嗯,序列化给了我类似 drive_style=1&... 但在输入名称中添加 '[]' 后,结果为空。
  • 好好阅读我的帖子 //php -> $('input[name=drive_style[]]').serialize();
  • 好吧,使用:$('input[name=drive_style[]]').serialize() 作为 post-data 我有相同的空字符串。
【解决方案3】:

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

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

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

【讨论】:

    【解决方案4】:

    正如其他人所提到的,您应该根据典型的“数组”命名约定来命名您的输入。 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);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      相关资源
      最近更新 更多