I'm trying to send a PHP array to Javascript.
So far i was only sending a single array in Json to my javascrpt ajax;
The PHP part so far works like that;
If I have an error in my request;
$data[0] = 'false';
$data[1] = 'Error message';
If there is no error;
$data[0] = 'OK';
$data[1] = 'html content to display';
I want to send multidimensional array like;
$data[0] = 'OK';
$data[1] = 'html content to display';
$data[1][0] = 'OK';
$data[1][1] = 'another html content to display';
The Javascript function that passes the request;
function load(action,container){
return new Promise(function (resolve, reject) {
//alert(container);
$.ajax({
url: '/ajax.php',
type: 'post',
dataType: 'json',
data: { 'query': 'true', 'action': action, 'container': container },
success: function (data, status) {
console.log(data[0] + ' : FOR ' + action);
if (data[0] === 'false') {
$("#errorBox").addClass('visible');
for (var i = 1; i < data.length; i++) {
$('#errMsg').append(data[i]+'<br>');
$('#console').prepend('<span class="consoleError">ERREUR : </span>' + data[i] + '<br>');
}
$("#errorBox").show();
}
else if (data[0] === 'OK') {
resolve(data);
$(document).ready(function () {
$('#console').prepend('<span class="consoleGood"> Statue: ' + data[0] + '</span> Requête: ' + action + ' Conteneur:' +container+ '<br>');
data.splice(0,1);
});
}
return;
},
error: function (xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
return;
}
}); // end ajax call
});//promise
};
On the PHP part I use ;
$data[0]= 'OK';
$data[1]= 'some content';
$data[1][0]= 'some more content';
echo json_encode($data);
I can get all the data contained in the first level of the array but not the second level.
The console log is showing me:
"<div class="doc_css"><div class="doc_status">active</div><h1>Mon document</h1><p>Le corp de mon document</p><div class="doc_post_nfo">Originalement posté par 1 le 2016-02-13 15:25:35<div class="doc_edit">Modifier</div></div></div>"
length: 1
__proto__: Array[0]
So what do I have to do the get the second level of the array?
console.log()
of the array in the JavaScript.$data[1][0]= 'some more content';
will rewrite what's in thedata[1]= 'some content'
. think of$data[1]
as of a box which can store either a value'some content'
more more boxes, which will be an another array