簡體   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