如何通过jQuery.ajax将JavaScript数组发送到PHP? [英] How to send an JavaScript array to PHP via jQuery.ajax?

查看:123
本文介绍了如何通过jQuery.ajax将JavaScript数组发送到PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jQuery.ajax将JavaScript数组发送到PHP页面,但是该数组仅使用空白值发送.

I'm trying to send a JavaScript array to a PHP page via jQuery.ajax, but the array is sent only with blank values.

如果我在Chrome上打开F12控制台并检查JS对象,则该对象在那里.全部填满.但是,当我使用ChromePhp工具记录PHP变量时,它仅显示空白值(此外,如果我遍历php数组并回显其值,则会得到所有空白).

If I open the F12 Console on Chrome and check for the JS object, it's there. All filled up. But when I log the PHP variable using the ChromePhp tool, it shows only the blank values (also, if I iterate over the php-array, echoing its values, I get all blanks).

我在这里很困惑.

这是我的示例代码:

<?php 
include 'ChromePhp.php';
if (isset($_GET['newUsers'])) {
    $newUsers = $_GET['newUsers'];
    ChromePhp::log($newUsers);
} else { ?>

<html>
<body>

    <script src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        var newUsers = [];

        newUser = [];
        newUser['nome'] = 'alvaro';
        newUser['idade'] = '34';
        newUsers.push(newUser);

        newUser1 = [];
        newUser1['nome'] = 'bia';
        newUser1['idade'] = '7';
        newUsers.push(newUser1);

        newUser2 = [];
        newUser2['nome'] = 'alice';
        newUser2['idade'] = '2';
        newUsers.push(newUser2);

        $.ajax({
            url: "testcookie.php",
            type: "GET",
            data: {
                'newUsers[]': newUsers
            }
        });

    </script>
</body>
</html>
<?php } ?>


已更新基于第一条评论.现在,我可以传递对象,但是不知道如何读取其属性.已经尝试$ user ['nome'],但没有结果.


Updated Based on the first comments. Now I get to pass the object, but don't know how to read its properties. Already tried $user['nome'] with no result.

<?php 
include 'ChromePhp.php';

if (isset($_POST['newUsers'])) {

    $newUsers = $_POST['newUsers'];

    foreach ($newUsers as $user) {
        # code...
        # HOW DO I READ THE nome AND idade VALUES HERE?
    }

} else { ?>

<html>
<body>

    <script src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        //var newUsersObj = {};
        var newUsers = [];

        newUser = {};
        newUser['nome'] = 'alvaro';
        newUser['idade'] = '34';
        newUsers.push(newUser);

        newUser1 = {};
        newUser1['nome'] = 'bia';
        newUser1['idade'] = '7';
        newUsers.push(newUser1);

        newUser2 = {};
        newUser2['nome'] = 'alice';
        newUser2['idade'] = '2';
        newUsers.push(newUser2);

        $.ajax({
            url: "testcookie.php",
            type: "POST",
            data: {
                'newUsers[]': newUsers
            },
            success: function () {

            },
            error: function () {

            }
        });

    </script>
</body>
</html>
<?php } ?>

推荐答案

知道了!

要正确访问PHP中的JavaScrtipt对象,在推送数组时,需要 JSON.stringify .然后,在PHP上,将它们 json_decode 转换为PHP对象,并使用'->'运算符访问其属性.

To properly access the JavaScrtipt objects in PHP I need to JSON.stringify them when pushing on the array. Then, on PHP, json_decode them to a PHP object, and access their properties with the '->' operator.

最终解决方案如下:

<?php 
include 'ChromePhp.php';

if (isset($_POST['newUsers'])) {

    $newUsers = $_POST['newUsers'];

    foreach ($newUsers as $user) {
        # code...
        $usr = json_decode($user);
        ChromePhp::log("Nome: " . $usr->nome . " - Idade: " . $usr->idade);
    }

} else { ?>

<html>
<body>

    <script src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        //var newUsersObj = {};
        var newUsers = [];

        newUser = {};
        newUser['nome'] = 'alvaro';
        newUser['idade'] = '34';
        newUsers.push(JSON.stringify(newUser));

        newUser1 = {};
        newUser1['nome'] = 'bia';
        newUser1['idade'] = '7';
        newUsers.push(JSON.stringify(newUser1));

        newUser2 = {};
        newUser2['nome'] = 'alice';
        newUser2['idade'] = '2';
        newUsers.push(JSON.stringify(newUser2));

        $.ajax({
            url: "testcookie.php",
            type: "POST",
            data: {
                'newUsers[]': newUsers
            },
            success: function () {

            },
            error: function () {

            }
        });

    </script>
</body>
</html>
<?php } ?>

这篇关于如何通过jQuery.ajax将JavaScript数组发送到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆