0

I want the data returned from an ajax post to be put into a javascript variable where I can then run an if statement checking whether that variable is equal to true. However, Firebug is telling me the variable verify is not defined. How do I write the function within the ajax post to set the data to verify correctly? Code is below.

$.post('ajax_file.php', 
{
user_id: user_id,    
band_term: band_term
}, function (data) {
var verify = data;              

if (verify == 'true') 

   {

   $('#request_form').hide();                  

   $('#where_to_go').hide();                            

   $('#change_form').show();                                    

}});

The ajax file returns true on success and false on failure.

if (mysql_query($sql) == true)

{ echo 'true';} else {echo 'false';}

Firebug shows me that the ajax file is returning with the string true, so I know the ajax file is working.

4 Answers 4

1
+50

The issue is on a few places. First, how you output data on you .php file. You should be returning JSON and accepting JSON on you ajax request. Look at this example:

<?php
    $variable = array("stat" => true, "data" => array(10, 10));
    print_r(JSON_Encode($variable));
?>

That will output this:

{"stat":true,"data":[10,10]}

Then on yout JS you'd do:

$.post('ajax_file.php', {
   user_id: user_id,    
   band_term: band_term
}, function (data) {
   //Data is the whole object that was on the response. Since it's a JSON string, you need to parse it back to an object.

   data = JSON.parse(data);

   if (data.stat === true){
      $('#request_form').hide();                  
      $('#where_to_go').hide();                            
      $('#change_form').show();                                    
   }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I'm receiving an error from Firebug. I'll work on the error and keep you up to date with what's happening. This is my very first time working with JSON so bear with me. SyntaxError: JSON.parse: unexpected character [Break On This Error] data = JSON.parse(data);
1

It's because verify was created in the callback function. Also, that variable isn't visible outside that function.

To operate on returned data from an AJAX call, do it in the callback function.

$.post('ajax.php', {
    user_id: user_id,
    term: term
}, function (data) {
    var verify = data;      //assuming data is just true or false
    if (verify === 'true') {
        unnecessary code
    }
});

2 Comments

Well, I tried it but it still does not work. I added the function from the ajax file thinking that may be the problem but I don't see any obvious ones. I doubled checked and the mysql_query should return true.
@jason328 data could be a string (I think jQuery doesn't transform it to boolean) so I updated my answer.
0

The variable is defined inside the callback function is does not match the scope of the document.

To make it actually work, just define it anywhere in the beginning of your script as follows:

var verify;



$.post('ajax.php', 
{
user_id: user_id,
term: term
}, 
function (data) 
{
    verify = data;    // then also remove the word var from your code here.  

       if (verify == 'true') 
       {unnecessary code}         
}
);          

4 Comments

Still does not work. I know that the data is being returned true but it appears that the if (verify == true) part is not working.
yes, because ajax post returns string, and === is a strict comparison for the type of boolean. So just change it to verify == 'true'
@Anonymous verify is still null by the time the condition is executed. your code won't work.
hell, common sense :D wait, does he mean to use that code literally? >.<
0

-i wouldn not use j query for ajax ( i find getdata to be better but the call back variable needs to be passed to the next function ie. if you are gonna alert(data) as your call back, do your if statements there.

also i was running into similar problems. using numbers such as one or zero in my php response helped alot, and i just used js to determine what the actual string or alert output would be

5 Comments

getdata? What are you referring to? You mean PHP Get Method?
var ajaxdestination=""; var edit = 'lvfavoritesedit.php?lat=' + lat + '&lng=' + lng; function getdata(what,where) { // get data from source (what) try { xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { /* do nothing */ }
// we are defining the destination DIV id, must be stored in global variable (ajaxdestination) ajaxdestination=where; xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV xmlhttp.open("GET", what); xmlhttp.send(null); return false; } function triggered() { // put data returned by requested URL to selected DIV if (xmlhttp.readyState == 4) if (xmlhttp.status == 200) document.getElementById(ajaxdestination).innerHTML =xmlhttp.responseText; } }
then call getdata('phpfile','thediv or span') as a functionn, in the call back where it says the ajax destination u can use if (xmlhttp.responseText = 1){ do this }else{do that}
Thanks for the help. I think I will stick with jQuery though since it is easier for me and the problem is fixed with the use of JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.