0

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!

satya
  • 1,889
  • 10
  • 29
  • 42

2 Answers2

1

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);
  }
});
Tomas
  • 5,067
  • 1
  • 35
  • 39
  • Yes. You can use the `$.ajax` instead of `.post` in JavaScript so that you can implement a seperate error function. See the JQuery docs for details. The `header` and `echo` functions are PHP to return the error to the client. The `$.ajax` call then knows that it should use the error function because of the http statuscode. – Tomas Dec 01 '10 at 13:16
0

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?

Community
  • 1
  • 1
Charles Ouellet
  • 6,338
  • 3
  • 41
  • 57