简体   繁体   English

通过jquery ajax将数组传递给php

[英]Passing an array through jquery ajax to php

I'm trying to turn form inputs into an array (input names as keys and values as values) inside of jquery using serializeArray()... then pass it to a php script via $jquery.ajax.. using the post method. 我试图使用serializeArray()将表单输入转换为jquery内部的数组(输入名称为键,值作为值)...然后使用post方法通过$ jquery.ajax ..将其传递给php脚本。

$(function () {
    $("#xbut").click(function () {
        var values = {};
        $.each($(':input').serializeArray(), function (i, field) {
            values[field.name] = field.value;
        });
        $.ajax({
            type: "POST",
            url: 'ajax/NewClient.php',
            data: {
                clientdata: values
            },
            success: function (data) {
                alert(data);
            }
        });
        return false;
    });
});

i had this working last night... but made some changes and now can't revert. 我昨晚做了这个工作...但是做了一些更改,现在无法还原。 when i use the array conversion for data... i cannot find $_POST['clientdata'] in the NewClient.php.. it just isn't defined. 当我将数组转换用于数据时...我在NewClient.php中找不到$ _POST ['clientdata']。.只是未定义。 If I change my ajax data into a string... i can find whatever i post. 如果我将ajax数据更改为字符串...我可以找到我发布的任何内容。

Any ideas? 有任何想法吗?

I'm pretty sure that 我很确定

.serializeArray()

should be 应该

.serialize()

$(function() {
    $("#xbut").click(function() {
        var values = {}; 

        $.ajax({
            type: "POST",
            url: 'ajax/NewClient.php',
            data: { clientdata : $(":input").serialize() },
            success: function(data){
                alert(data);
            }
        });
    return false;
    });
});

I've tested your script and it seems to work - 我已经测试了您的脚本,它似乎可以正常工作-

<!DOCTYPE html>
<html lang="en">
    <head>
        <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <script>
        $(function() {
            $("#xbut").click(function() {
                var values = {};
                $.each($(':input').serializeArray(), function(i,field){
                    values[field.name] = field.value; 
                }); 
                $.ajax({
                    type: "POST",
                    url: 'PHPPage3.php',
                    data: { clientdata : values },
                    success: function(data){
                        alert(data);
                    }
                });
            return false;
            });
        });
    </script>
    <body>
    <input type="text" name="tester"/>     
    <input type="button"  value="x" id="xbut"/>   
    </body>
</html>

Then 'PHPPage3.php' is - 然后“ PHPPage3.php”是-

<?php
    echo $_POST['clientdata']['tester'];
?>

On clicking the button on the first page the value in the 'tester' field is alerted back. 单击第一页上的按钮后,“测试器”字段中的值将被提醒。

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

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