Reputation: 6316
I have a PHP file which is responding to a jQuery Ajax call with dataType: 'HTML',
. The PHP looks like
if( $result->num_rows > 0 ){
....
echo '<p>There are some user already </p>';
}
else{
echo '<p>List is empty </p>';
}
this is working fine on my JS side like
ajaxcall.done(function(data) {
$('#call-result').html(data);
});
but I also need to add some business logic on client side to the page by passing a Boolean flag from the server to the JS. How can I pass the true false along with HTML snippet from the server to client?
Upvotes: 2
Views: 101
Reputation: 42695
Just use JSON to respond:
<?php
if ($result->num_rows > 0) {
$html = '<p>There are some user already </p>';
$result = false;
} else{
$html = '<p>List is empty </p>';
$result = true;
}
$response = ["html"=>$html, "result"=>$result];
header("Content-Type: application/json");
echo json_encode($response);
Then, in your JS:
ajaxcall.done(function(data) {
var result = data.result;
$('#call-result').html(data.html);
});
jQuery will automatically parse a response of type json
into a JavaScript object so you can access the elements directly.
Upvotes: 1