0

I currently have the following code :

function Parse_Error(ErrMsg) {

$.post("ajax/errormsg.php", {
    errmsg: ErrMsg} 
    , function(data) {
    alert (data);
                return (data);
});
}

The alert will show me the correct message, but the function doesn't return the message. The function kept returning "undefined" but the alert is working perfectly. I've triedto add the following :

var tmp = data;
return tmp;

Without success.. Where did I go wrong?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Daniel Berthiaume
  • 100
  • 1
  • 3
  • 14

1 Answers1

0

The problem is the return statement isn't returning for the Parse_Error function. It's returning for the so-called anonymous function you declare (function(data){...}) which gives the data to JQuery, which it effectively ignores. Instead, you need to do something like this:

// your code that does the query
do_error_query('The Error Message');

function do_error_query(ErrMsg) {
    $.post("ajax/errormsg.php", {errmsg: ErrMsg} , function(data){

        alert(data);
        // here you need to call a function of yours that receives the data.
        // executing a return statement will return data to jquery, not to the calling
        // code of do_error_query().
        your_function(data);

    });
}

function your_function(data){
    // process the data here
}

The problem is that the call to do_error_query() completes BEFORE the result from the PHP page even comes back. So there is no way the result could possibly be returned. In other words, your_function(data) would be called well after do_error_query() returns. I hope that makes sense.

Effectively, do_error_query() is merely setting up an event handler. It cannot return the value, because the event has not completed yet. That is what the event handler, your_function(data) is for. It handles the event, the returning of data from your PHP page. While the event is yet to complete, do_error_query() will have long finished.

Jodes
  • 14,118
  • 26
  • 97
  • 156
  • (I'm not at home to test) but in theory, if I undestood correctly, – Daniel Berthiaume Feb 27 '13 at 17:45
  • Hmm.. That dopes complicated my problem then! AS I have multiple language on my web application, therefore when I have a client side error message, I call the same php script and return the correcte error message. EX: In the script I'll send the following string to the php script "{Client_Not_Found}" and the script will return "The Requested Client was not found in the database". Of course, if the user is Speack french or spanish, the text come back in his language. I currently use the same code all voer my script and wanted to group it in a function... Any good idea? Thank! – Daniel Berthiaume Feb 27 '13 at 17:52
  • It is natural that some things will be duplicated on the client and server, the solution is one of personal preference and also depends on the size of the application, if parts are going to be reused or not etc etc. My choice, if supporting just a few languages and just a few error messages, I would hard code them, duplicating where necessary. But if you already develop a structure to cater for many strings, or even articles, in different languages, then you could use that instead. – Jodes Feb 27 '13 at 20:50
  • Thank for all the input, Sin I'll be using this code ofter, I'll still be using the function, but will use the function to alert the message to the client instead of resending the data through the function. thank again for your help! – Daniel Berthiaume Feb 28 '13 at 01:09