【问题标题】:Why input type text are array in post jQuery为什么输入类型的文本在后 jQuery 中是数组
【发布时间】:2016-01-17 13:43:23
【问题描述】:

我有这样的表格

    <form id="facform" action="exec/sendfac.php" method="post">
    <input id="mbr_id" class="adresse" name="facdatas[][mbr_id]" type="text" value="" readonly="readonly" />
    <input id="nom" class="adresse" name="facdatas[][nom]" type="text" value="" readonly="readonly" />
    <input id="adresse" class="adresse" name="facdatas[][adresse]" type="text" value="" readonly="readonly"/>
    <input id="ville" class="adresse" name="facdatas[][ville]" type="text" value="" readonly="readonly"/>
    <input name="facdatas[][concerne]" class="concern" value="" type="text">
    <input name="ligneFac[][designation]" type="text" class="facBig" />
    <input name="ligneFac[][unite]" type="text" class="facSmall" />
    <input name="ligneFac[][quantite]" type="text" class="facSmall" />
    <input name="ligneFac[][prixUnite]" type="text" class="facSmall" value="" />
    <input name="ligneFac[][taxe]" type="text" class="facSmall" value="" />
    <input name="ligneFac[][totLine]" type="text" class="facSmall" value="" />
    <input id="send" type="submit" name="submit" value="Envoyer" />

这个表单是用这个 jQuery 发布的

$("#facform").submit(function(e)
{
    var nbr_tr = $('#fac_table tr').length;

    var sendURL = $(this).attr("action");
    var sendPost = $(this).attr("method");
    var facArray = $("#facform").serializeArray();

    $.ajax(
    {
        method: "POST",
        url: sendURL,
        data: facArray,
        success: function(response)
        {
            console.log(response);
            $("#facform").submit();
        }
    });
});

结果给我一个这样的每个文本的数组:

Array ( [facdatas] => Array ( [0] => Array ( [mbr_id] => 26 ) [1] => Array ( [nom] => Gautier Albert ) [2] => Array ( [adresse] => Avenue du Devin du Village 51 ) [3] => Array ( [ville] => 1406 Cronay ) [4] => Array ( [concerne] => TEST ) [5] => Array ( [totalFac] => 69.00 ) ) [ligneFac] => Array ( [0] => Array ( [designation] => Pantalon de training CPNS ) [1] => Array ( [unite] => pièces ) [2] => Array ( [quantite] => 1 ) [3] => Array ( [prixUnite] => 69 ) [4] => Array ( [taxe] => 0.00 ) [5] => Array ( [totLine] => 69.00 ) [6] => Array ( [designation] => ) [7] => Array ( [unite] => ) [8] => Array ( [quantite] => ) [9] => Array ( [prixUnite] => ) [10] => Array ( [taxe] => 0.00 ) [11] => Array ( [totLine] => 0.00 ) [12] => Array ( [designation] => ) [13] => Array ( [unite] => ) [14] => Array ( [quantite] => ) [15] => Array ( [prixUnite] => ) [16] => Array ( [taxe] => 0.00 ) [17] => Array ( [totLine] => 0.00 ) [18] => Array ( [designation] => ) [19] => Array ( [unite] => ) [20] => Array ( [quantite] => ) [21] => Array ( [prixUnite] => ) [22] => Array ( [taxe] => 0.00 ) [23] => Array ( [totLine] => 0.00 ) [24] => Array ( [designation] => ) [25] => Array ( [unite] => ) [26] => Array ( [quantite] => ) [27] => Array ( [prixUnite] => ) [28] => Array ( [taxe] => 0.00 ) [29] => Array ( [totLine] => 0.00 ) ) [submit] => Envoyer ) 

似乎每个字符串都是一个数组,如果我要 print_r($_POST['data']) 它是空的...

第二个问题是如何在没有空行的php中使用这个数组?

有什么想法吗??

感谢您的帮助

【问题讨论】:

    标签: php jquery arrays forms multidimensional-array


    【解决方案1】:

    您没有给出任何数据变量,因此它是空的。 可以修改ajax代码为

    $.ajax(
        {
            method: "POST",
            url: sendURL,
            data: {data: facArray},
            success: function(response)
            {
                console.log(response);
                $("#facform").submit();
            }
        });
    

    然后你可以让 $_POST['data'] 包含输入。否则 $_POST 将只有所有的输入,而不是数据。

    【讨论】:

    • 好的,谢谢,现在我可以拥有我的数组了,但是如果我使用 array_filter,结果总是空行,你知道如何删除空行吗?
    • 你能解释一下吗?在哪一行。
    • 它在我的 exec/sendfac.php 中
    • 现在如果我在成功函数 window.location.href = sendURL; 提交表单后在 ajax 中添加这一行结果是 Array() ;你知道怎么做吗?
    • 这里是代码:print_r(array_filter($_POST['data']));
    猜你喜欢
    • 2018-03-06
    • 1970-01-01
    • 2015-10-12
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多