Diciamo che ho una matrice di oggetti javascript, e sto cercando di passare quegli oggetti a una pagina php per salvarli in un database. Non ho problemi a passare una variabile al php e ad usare $ _POST ["entries"] su quella variabile, ma non riesco a capire come passare un'intera serie di oggetti, così posso accedere ai miei valori objects.entryId e .mediaType sulla pagina php.Come passare un array di oggetti javascript a php usando POST
Oh e prima che qualcuno chieda, sì, la ragione per cui ho bisogno di farlo in questo modo è perché ho un uploader flash, che hai indovinato .. carica in un server CDN (remoto) e il server remoto risponde solo con tali oggetti js.
Grazie per qualsiasi aiuto chiunque può fornire.
Ecco le mie funzioni JS:
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 < entries.length; i++) {
parameterString += (i > 0 ? "&" : "")
+ "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");
}
}
}
btw ... mi dispiace per i collegamenti! – coma