I'm trying to send data using AJAX to a PHP page (which queries a database) and return specific values. I then will display the results.
I've found several examples on how to pass data using AJAX. This is not my problem. It's how to get the multiple results from a SQL query back to the original page.
page2.php
$prod01 = $_REQUEST['prod01'];
$prod02 = $_REQUEST['prod02'];
$cost01 = mysqli_fetch_array(mysqli_query($db,"SELECT `cost`,`cost2` FROM `info` WHERE `blah`='".$prod01."'"));
$cost02 = mysqli_fetch_array(mysqli_query($db,"SELECT `cost`,`cost2` FROM `info` WHERE `blah`='".$prod02."'"));
$getcost01 = $qty01 * $cost01[0];
$getcost02 = $qty02 * $cost02[0];
$foo = array('result01' => $getcost01, 'result02' => $getcost02);
echo json_encode($foo);
page1.php
var prod01 = $("#prod01").val();
var prod02 = $("#prod02").val();
$.ajax({
type: "POST",
url: "page2.php",
dataType: 'json',
data : { prod01 : 'pro01', prod02 : 'prod02'},
cache: false,
success: function(result){
$('.showit01').text(result.result01);
$('.showit02').text(result.result02);
}
});
The result of the above is:
$getcost01
$getcost02
Not the expected SQL result from database.
When I navigate to "/page2.php?prod01=ABC&prod02=DEF" it displays as expected:
{"result01":"100","result2":"200"}