Unable to get posted array from jQuery


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


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 <em>myurl.php</em>:

<?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); });

<strong>This doesn't work!</strong> 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.


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



  • How to save dynamically created textboxes and their values
  • Less Conflicting Session Manager for Zope 2
  • pip in virtualenv gets ConnectTimeoutError
  • Group list of tuples by item
  • Using Sax parsing to edit and write XML in VB6
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Run multiple queries from 1 SQL file showing result in multiple tables
  • Does Mobilefirst provide a provision to access web services directly?
  • How to use JavaScript to determine whether a file exists in a directory?
  • Installed module is empty
  • Functions in global context
  • jQuery .attr() and value
  • Django: Count of Group Elements
  • FileReader+canvas image loading problem
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Different response to non-authenticated users and AJAX calls
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Web-crawler for facebook in python
  • Do I've to free mysql result after storing it?
  • Compare two NSDates in iPhone
  • A cron job substitute?
  • json Serialization in asp
  • trying to dynamically update Highchart column chart but series undefined
  • embed rChart in Markdown
  • apache spark aggregate function using min value
  • Django query for large number of relationships
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?