tags:

views:

84

answers:

3

I have an ajax query sending to a php script and putting the response into a div. The code is as follows:

$.ajax({
type: "POST",
url: "core/process.php",
data: data,
success: function(response){
   $("#form").fadeOut('slow');
   alert("DO NOT LEAVE THIS PAGE UNTIL COMPLETE");
   $("#response").html("DO NOT LEAVE THIS PAGE UNTIL COMPLETE<br />")
   .append(response)
   .hide()
   .slideDown('slow');
}
});

What I want to do is check the response for a string or return such as "true" or "success" or whatever and alert the user the script is complete. I know very little jQuery beyond what you see above and am not sure how to search a response for something specific.

Edit: I just realized I forgot to put how the script outputs the response. It's just echoing in the php script. I'm thinking I may need to put everything into an array and json_encode it, but that's just a guess.

+2  A: 

You could use the indexOf function:

if (response.indexOf('success') > -1 || response.indexOf('true') > -1) {
    // the response contains one or both of the strings success and true
}
Darin Dimitrov
This worked perfectly. Thank you.
RedElement
A: 
if (/Success/.test(response)) {
  alert('Cool')
}

Or change the response type to json and have the server encode a status code and output string as the result object.

jspcal
A: 

i too have the similar question. what does response really contain??

in my php file i used die() for error checking. so if there is any condition which does not satisfy then i need to show it to the user. for example i have a check for repeated username and email Id. at server side, it checks for these condition. Now how do i get these into my success: function() event??

thanks in advance

noobcode
response is just what I named my callback. From my (limited) understanding of it, the response will contain what is returned, echoed, or json encoded. Again, I'm really new to jQuery so this could be wrong.
RedElement