0

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....

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" ??

2 Answers 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.

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[].

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.

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

Asume you have a file 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".

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

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.