I am having a hard time passing arrays from an PHP page to JavaScript using an AJAX request.
I will have to pass the information from multiple php arrays to javascript. I am aware I can use json_encode for this, however, I am having a hard time implementing this. The $name
array don't seem to be getting passed, in addition, I will need all arrays to be passed to javascript not just $name
.
I appreciate any suggestions with this.
Many thanks in advance!
This is what I tried for passing the $name
array (code snippets):
PHP
while($row2 = mysqli_fetch_array($results2)){
$name[$i] = $row2['prod_name'];
$price[$i] = $row2['price'];
$upc[$i] = $row2['upc'];
$quantity[$i] = $row2['quantity'];
}
echo json_encode($name);
AJAX
$.ajax({
url: "invoice-get-data.php?hotItems=1&getArrays=1",
dataType: "json",
success: function(data){
alert(data[0]);
}
});
zavg :
Back-end:\n\nwhile($row2 = mysqli_fetch_array($results2)){\n $name[] = $row2['prod_name'];\n $price[] = $row2['price'];\n $upc[] = $row2['upc'];\n $quantity[] = $row2['quantity'];\n}\necho json_encode(array($name, $price, $upc, $quantity));\n\n\nFront-end:\n\n$.ajax({\n url: \"invoice-get-data.php?hotItems=1&getArrays=1\",\n dataType: \"json\",\n success: function(data){\n data = JSON.parse(data);\n alert(data[0]);\n }\n});\n",
2013-04-14T16:53:15