Reputation: 1959
Hallo, i am new to jquery, AJAX requests. Here is my html code in edit_page.php
<td>Action: </td>
<td class="notselected">
<div id="action_response"></div>
<select name="action_db1" id=<?php echo $sel_page['Concession']; ?> onchange="updateaction(this);" >
<option value=""></option>
<option value="move">Move</option>
<option value="copy">Copy</option>
<option value="exclude">Exclude</option>
</select>
</td>
and the javascript code is
function updateaction(item) {
$.post("edit_page_advanced_actions.php", {concession:item.id, action:item.value, db_name:item.name},function(action_response) {
$('#action_response').html(action_response);
});
}
Here i am calling php script *edit_page_advanced_actions.php*
in which i wrote some php code. I want to return messages from here based on the database updation to edit_page.php. i.e from the calling script.
EDIT:-
I am updating database in edit_page_advanced_actions.php. I want to return an error message or success message to edit_page.php. i.e from where this is triggered. For example "successfully updated", "Copied successfully","Failed to exclude" to the users based on db operations.
How this can be accomplished.
Thanks in advance!
Upvotes: 0
Views: 132
Reputation: 5143
You should return the error status in the http header using the php header
function. For example:
header("Status: 400 Bad Request");
echo "The error is: ....";
You can then use$.ajax
with a special error function:
$.ajax({
type: 'POST',
url: "edit_page_advanced_actions.php",
data: {
concession:item.id,
action:item.value,
db_name:item.name
},
succes: function(data, action_response) {
$('#action_response').html(action_response);
},
error: function(xhr, action_response) {
// error code goes here
alert(action_response);
}
});
Upvotes: 1
Reputation: 6518
I suggest you return a json object in your server side method and use it as a response in your client side ajax call.
You could return a json object like
{ Success: true, ErrorMessage: 'An error happened' }
Because you're doing a $.post your action_response variable would be a json string, you will need to parse it into a javascript object.
Look at this for parsing the json response: http://api.jquery.com/jQuery.parseJSON/
and in your client side code:
if(!action_response.Success) {
displayErrorMessage(action_response.ErrorMessage);
}
EDIT:
Look at this post to return a json object from PHP method
Returning JSON from PHP to JavaScript?
Upvotes: 0