serialise数组将输入相同的名称

时间:2011-09-13 14:19:11

标签: php jquery arrays serialization post

我有3个输入,其中2个我想要在同一个数组中(name =“notes ['a']”和name =“notes ['b']”)但是当我使用jquerys的serializeArray时( )它将它们全部置于同一“水平”。但是我想使用php将已发布的notes数组序列化到数据库中,使用的方法是为不属于数组的输入工作(即name =“basic”

foreach($_POST['data'] as $var => $value) {

    if(!strstr(strtolower($value['name']),'added')) $q1 .= "".$value['name']."='".$value['value']."',";

}

echo $q = "UPDATE ".$_POST['table']." SET ".rtrim($q1,',')." WHERE ".$_POST['idField']."=".$_POST['id']." ";    


*the array $_POST['data'] is getting posted as;*
 [data] => Array
        (
            [0] => Array
                (
                    [name] => notes[\'a\']
                    [value] => dan@jynk.net
                )

            [1] => Array
                (
                    [name] => notes[\'b\']
                    [value] => Daniel Crabbe
                )

            [2] => Array
                (
                    [name] => test
                    [value] => Daniel Crabbe
                )

        )

但我想它应该沿着这些方向?

[data] => Array
        (
            [0] => Array{
                Array(
                    [name] => notes[\'a\']
                    [value] => dan@jynk.net
                )
                Array
                (
                    [name] => notes[\'b\']
                    [value] => Daniel Crabbe
                )
            )


            [1] => Array
                (
                    [name] => test
                    [value] => Daniel Crabbe
                )

        )

如何让jquery尊重输入数组中的数组?任何帮助欢迎...

更新 没有这样,但希望所有级别[0]上的所有内容都能轻松访问...

Array
(
    [0] => Array
        (
            [name] => cm_email_to
            [value] => dan@jynk.net
        )

    [1] => Array
        (
            [name] => cm_name_to
            [value] => Daniel Crabbe
        )

    [2] => Array
        (
            [name] => cm_moveToList
            [value] => 
        )

)

*inputs*
<input id="cm_email_to" name="cm_email_to" value="dan@jynk.net" class="toPostCM" />
<input id="cm_name_to" name="cm_name_to" value="Daniel Crabbe" class="toPostCM" />

*jquery*
var dataCMSettings = $(".toPostCM").serializeArray();

3 个答案:

答案 0 :(得分:3)

尝试从输入名称中取出数组索引:

<input name="notes[]" value="val1" />
<input name="notes[]" value="val2" />

答案 1 :(得分:0)

从HTML中删除数组索引中的引号,array['a']变为array[a]

答案 2 :(得分:0)

好的 - 这正是我需要的......

var params = {};

$('.toPostCM').each(function(index,value) {
    params[value.name] = value.value;
});

console.log(params);

*gives me*
Array
(
    [cm_email_to] => dan@jynk.net
    [cm_name_to] => Daniel Crabbe
    [cm_moveToList] => 
)

感谢所有......