I have a button to call a php file with jQuery in my html file,
<script>
$('#mysubmit').bind('click', function(e){e.preventDefault();
var jqueryXHR = $.ajax({
'type': 'POST',
'url': 'http://localhost/lt/resources/lists/update.php',
'dataType': 'json'
});
</script>
<input type="submit" id="mysubmit" value="Submit" />
It works but I don't know how to get the result of the php file, i.e. the php file works, make an API POST and get the result as success or error, but I don't know how to get through jQuery if the php file response is success or error.
Please help me I am a noob in Ajax and Jquery.
Try adding a success \ error handler to see the response:
var jqueryXHR = $.ajax({
type: 'POST',
url: 'http://localhost/lt/resources/lists/update.php',
dataType: 'json',
success: function(data) {
console.log(data);
}
});
You should however follow @skobaljic's suggestion, and use a recognized response format such as JSON or XML - to make it easier to parse and more professional.
Using PHP it would look something like this:
<?php
$result = ..... whatever you like to return here ...
header('Content-Type: application/json');
echo json_encode($result);
?>
use jquery ajax success property.
<script>
$('#mysubmit').bind('click', function(e){e.preventDefault();
var jqueryXHR = $.ajax({
'type': 'POST',
'url': 'http://localhost/lt/resources/lists/update.php',
'dataType': 'json',
'success' : function(returnData){
alert(returnData); // or
console.log(returnData);
}
});
</script>
<input type="submit" id="mysubmit" value="Submit" />
what every you print at PHP side it will return in "success" function
your php file must respond with an echo:
<?php echo "success"; ?>
in your request do something like this:
$.ajax({
'type': 'POST',
'url': 'http://localhost/lt/resources/lists/update.php',
'dataType': 'json'
}).done(function(response){ /*DO SOMETHING WITH response*/ });