无法从jQuery获取发布的数组

时间:2022-04-17 12:14:38

I am trying to post a group of arrays using the jQuery post method, but I am having trouble getting the value of the arrays. How can I get the values of the array that I have sent? If somebody could help me i would be grateful....

我试图使用jQuery post方法发布一组数组,但是我无法获得数组的值。如何获取已发送的数组值?如果有人能帮助我,我会很感激....

Here is what i have done:

这就是我所做的:

    $(document).ready( function()
        {
            $("#submit_info").click (
                function()
                {
                    var batchArr= new Array();
                    batchArr=arrPush('batch');
                    var facultyArr= new Array();
                    facultyArr=arrPush('faculty');
                    var levelArr= new Array();
                    levelArr=arrPush('level');
                    var sectionArr= new Array();
                    sectionArr=arrPush('section');
                    var shiftArr= new Array();
                    shiftArr=arrPush('shift');

                    $.post("server_side/college_info_insert.php",{
                            batchArr:batchArr,
                            facultyArr:facultyArr,
                            levelArr:levelArr,
                            sectionArr:sectionArr,
                            shiftArr:shiftArr
                        }, function(data)
                        {
                            alert(data);
                        });
                }
            );

            function arrPush(opt)
            {
                var Arr= new Array();
                Arr.push($("#"+opt+"_1").val());
                var count= $("#"+opt).val();
                var i=0;
                for(i;i<=count;i++)
                {
                    if(i==0)
                    {
                        Arr.push($("#txt"+opt).val());
                    }
                    else
                    {
                        Arr.push($("#txt"+opt+i).val());
                    }
                }
                return Arr;
            }
        }
    );

How can I get the array values in the next page "college_info_insert.php" ??

如何在下一页“college_info_insert.php”中获取数组值?

2 个解决方案

#1


okay, so this is actually a really common issue. It's unclear that you can't just send an array as-is from javascript to PHP and have it recognized.

好的,所以这实际上是一个非常普遍的问题。目前还不清楚你不能只是将一个数组从javascript发送到PHP并让它得到认可。

The problem is that PHP doesn't know how to read in multiple values from a POST request - typically things like that require the PHP author to use brackets like: varname[].

问题是PHP不知道如何从POST请求中读取多个值 - 通常这样的事情需要PHP作者使用括号,如:varname []。

So, basically you must send variables as strings. Using JSON you can send even complicated objects as strings to PHP using a single variable name. Typically you'd use JSON.stringify or something along those lines - but if you have a simple array you might not even need it.

所以,基本上你必须将变量作为字符串发送。使用JSON,您甚至可以使用单个变量名称将复杂对象作为字符串发送到PHP。通常你会使用JSON.stringify或类似的东西 - 但如果你有一个简单的数组,你可能甚至不需要它。

Here's a full example of the problem/solution, found using jquery and $.post:

这是使用jquery和$ .post找到的问题/解决方案的完整示例:

Asume you have a file myurl.php:

假设你有一个文件myurl.php:

<?php
print_r($_POST);
?>

And in a separate file (or the console), you try:

在单独的文件(或控制台)中,您尝试:

var myarray = Array('some','elements','etc');
var mydata = {postvar1: 'string', postvar2: myarray};
$.post('myurl.php', mydata, function(response) { 
    alert("response: " + response);
});

This doesn't work! The result is that postvar2 only contains "etc".

这不起作用!结果是postvar2只包含“etc”。

The solution is force the array into a JSON string that can be decoded from PHP.

解决方案是将数组强制转换为可以从PHP解码的JSON字符串。

var myarray = Array('some','elements','etc');
var json_array = $.map(myarray,function(n) {
    return '"'+n+'"';
});
var mydata = {postvar1: 'string', postvar2: '['+json_array+']' };
$.post('myurl.php', mydata, function(response) { 
    alert("response: " + response);
});

ON the PHP side you now must use: json_decode($_POST['myarray']); to get your results in a proper array.

在PHP方面,你现在必须使用:json_decode($ _ POST ['myarray']);将结果放在合适的数组中。

Note, this example assumes very simple strings or numbers in your array. If you have complex objects, you will need to use a JSON.stringify function which will take care of extra quotes, escaping special characters, etc.

请注意,此示例假定数组中的字符串或数字非常简单。如果您有复杂的对象,则需要使用JSON.stringify函数来处理额外的引号,转义特殊字符等。

#2


Not sure if i understood the question but wouldn't something like this work

不确定我是否理解了这个问题,但不会像这样的工作

if (isset($_POST['batchArr'])) {
   $batchArr = $_POST['batchArr'];
   // Then populate your html here e.g
   echo $batchArr[0];

}

#1


okay, so this is actually a really common issue. It's unclear that you can't just send an array as-is from javascript to PHP and have it recognized.

好的,所以这实际上是一个非常普遍的问题。目前还不清楚你不能只是将一个数组从javascript发送到PHP并让它得到认可。

The problem is that PHP doesn't know how to read in multiple values from a POST request - typically things like that require the PHP author to use brackets like: varname[].

问题是PHP不知道如何从POST请求中读取多个值 - 通常这样的事情需要PHP作者使用括号,如:varname []。

So, basically you must send variables as strings. Using JSON you can send even complicated objects as strings to PHP using a single variable name. Typically you'd use JSON.stringify or something along those lines - but if you have a simple array you might not even need it.

所以,基本上你必须将变量作为字符串发送。使用JSON,您甚至可以使用单个变量名称将复杂对象作为字符串发送到PHP。通常你会使用JSON.stringify或类似的东西 - 但如果你有一个简单的数组,你可能甚至不需要它。

Here's a full example of the problem/solution, found using jquery and $.post:

这是使用jquery和$ .post找到的问题/解决方案的完整示例:

Asume you have a file myurl.php:

假设你有一个文件myurl.php:

<?php
print_r($_POST);
?>

And in a separate file (or the console), you try:

在单独的文件(或控制台)中,您尝试:

var myarray = Array('some','elements','etc');
var mydata = {postvar1: 'string', postvar2: myarray};
$.post('myurl.php', mydata, function(response) { 
    alert("response: " + response);
});

This doesn't work! The result is that postvar2 only contains "etc".

这不起作用!结果是postvar2只包含“etc”。

The solution is force the array into a JSON string that can be decoded from PHP.

解决方案是将数组强制转换为可以从PHP解码的JSON字符串。

var myarray = Array('some','elements','etc');
var json_array = $.map(myarray,function(n) {
    return '"'+n+'"';
});
var mydata = {postvar1: 'string', postvar2: '['+json_array+']' };
$.post('myurl.php', mydata, function(response) { 
    alert("response: " + response);
});

ON the PHP side you now must use: json_decode($_POST['myarray']); to get your results in a proper array.

在PHP方面,你现在必须使用:json_decode($ _ POST ['myarray']);将结果放在合适的数组中。

Note, this example assumes very simple strings or numbers in your array. If you have complex objects, you will need to use a JSON.stringify function which will take care of extra quotes, escaping special characters, etc.

请注意,此示例假定数组中的字符串或数字非常简单。如果您有复杂的对象,则需要使用JSON.stringify函数来处理额外的引号,转义特殊字符等。

#2


Not sure if i understood the question but wouldn't something like this work

不确定我是否理解了这个问题,但不会像这样的工作

if (isset($_POST['batchArr'])) {
   $batchArr = $_POST['batchArr'];
   // Then populate your html here e.g
   echo $batchArr[0];

}