Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass a javascript object array to php using POST
    text
    copied!<p>Lets say I have an array of javascript objects, and I am trying to pass those objects to a php page to save them into a database. I have no problems passing a variable to the php and using $_POST["entries"] on that variable but I can't figure out how to pass an entire array of objects, so I can access my objects.entryId and .mediaType values on the php page.</p> <p>Oh and before anyone asks, yes the reason I need to do it this way is because I have a flash uploader, that you guessed it.. uploads into a CDN server (remote) and the remote server only replies back with such js objects.</p> <p>Thanks for any help anyone can provide.</p> <p>Here is my JS functions:</p> <pre><code>function test() { entriesObj1 = new Object(); entriesObj1.entryId = "abc"; entriesObj1.mediaType = 2; entriesObj2 = new Object(); entriesObj2.entryId = "def"; entriesObj2.mediaType = 1; var entries = new Array(); entries[0] = entriesObj1; entries[1] = entriesObj2; var parameterString; for(var i = 0; i &lt; entries.length; i++) { parameterString += (i &gt; 0 ? "&amp;" : "") + "test" + "=" + encodeURI(entries[i].entryId); } xmlhttp.open("POST","ajax_entries.php",true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xmlhttp.setRequestHeader("Content-length", parameterString.length); xmlhttp.setRequestHeader("Connection", "close"); xmlhttp.onreadystatechange = handleServerResponseTest; xmlhttp.send(parameterString); } function handleServerResponseTest() { if (xmlhttp.readyState == 4) { if(xmlhttp.status == 200) { alert(xmlhttp.responseText); } else { alert("Error during AJAX call. Please try again"); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload