Well i have this ajax code which will return the result from MySql in Success block.
$.ajax({
type:"POST",
url:"index.php",
success: function(data){
alert(data);
}
});
My Query
$sql = "SELECT * FROM tablename";
$rs=parent::_executeQuery($sql);
$rs=parent::getAll($rs);
print_r($rs);
return $rs
My Response Array in alert of Success AJAX
Array
(
[0] => Array
(
[section_id] => 5
[version] => 1
[section_name] => Crop Details
[id] => 5
[document_name] => Site Survey
[document_master_id] => 1
[document_section_id] => 5
)
[1] => Array
(
[section_id] => 28
[version] => 1
[section_name] => Vegetative Report
[id] => 6
[document_name] => Site Survey
[document_master_id] => 1
[document_section_id] => 28
)
)
I want to get only section_name and document_name from the result so that i can append these two values to my list.
You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });
$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });
Don't return the response using print_r()
, use json_encode()
:
echo json_encode($rs);
Then in the Javascript, you can do:
$.ajax({
type:"POST",
url:"index.php",
dataType: 'json'
success: function(data){
for (var i = 0; i < data.length; i++) {
console.log(data[i].section_name, data[i].document_name);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With