views:

32

answers:

3

I am sending an ajax call to another page. I want to get a value of a variable, lets call it x, back from that page on success. How can I do that. here is my ajax code

$.ajax({
        type: 'POST',
        url: 'myotherpage.php',
        data: 'loginname=' + loginname ,
        success: function(success) {
            if(success == 1) {

            //get the variable value here           
           } else {

           //do nothing

            }
        }
    });
+1  A: 

I don't know what you are trying to do but to give you an idea,

if(success == 1) {
// codes get executed here if myotherpage.php would display 1
// so I'm wondering how would you create a variable there...
// if you put anything other than just '1' in myotherpage.php, codes inside this "if" will not be excuted

//get the variable value here           
} else {

//do nothing

}

in this,

success: function(data) {...} // "data" is the data being returned from the server
Reigel
+1  A: 

echo out the variable in your php file instead of "1" and have it return null when something is wrong.

$.ajax({
  type: 'POST',
  url: 'myotherpage.php',
  data: 'loginname=' + loginname ,
  success: function(success) {
    if(success == '') {
      // error alert
      alert('Something went wrong. Reload the page and try again.');
     } else {
      alert(success); // alert the value from what you printed out in myotherpage.php
     }
  }
});
Marwelln
+1  A: 

Your other page should return json, which contains a status variable (1 for success, 0 for fail), and the variable or whatever data you need. Here's an example from a file I have here. It won't run of course, but should give you the idea.

            Req = $.ajax({
                type: 'POST',
                data: this.data.filter,
                url: this.data.DataURL+"listids",
                dataType: 'json',
                timeout: 5000,
                cache: false,
                error: function(){
                    UserNotify({class:'notify_alert', content:'Your request can\'t be completed at this time.<br />An external error has been encountered.  Please wait a moment and try again.'});
                },
                success: function(o){
                    if ( 0==o.status ) {
                        if ( undefined == o.user_msg ) { o.user_msg = '';}
                        UserNotify({class:'notify_alert', content:'Your request can\'t be completed at this time.<br />'+o.user_msg});
                    } else {
                        if ( 0 < o.data.ids.length ) {
                            tli.data.update.ids = o.data.ids;
                        }
                    }
                }
            });
Eli
Eli is right. The function(success) part has the data as you named "success " in the parameter (which is confusing). So 'success' is what you would return to your view as json usually, so in your case you send back a variable to the view and you named it success. So if you want to check its value then you would do it as you normally would. But if you want to check if success failed and do some handling of error, it is suffice to write the error part. And use status to check status of the data. error:function () {...
Ehsan
@Ehsan - Sort of, but not quite. The error function will be run if there is an ajax error. This is handled internally by jQuery. If there is no ajax error, the success function will run with whatever data was returned passed as a single variable. In this case, that is a PHP array passed through json_encode(). I just call it o for object. It's json, and can be accessed by o.path.to.whatiwant. In my case, o.status says whether the php method was successful, and then o.data.ids (a list of ids) are assigned where I need them
Eli