views:

246

answers:

3

hi im trying to post a group of array using jquery post method but , am having trouble getting the value of those array 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" ??

A: 

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

}

David Archer
sorry didn't work but thanks for the ans ;)
jarus
A: 

Do a

print_r($_POST);

And tell us the output.

Daniel S
the output is Array(window object)
jarus
+1  A: 

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.

FilmJ