Dirty Bird Design
Dirty Bird Design

Reputation: 5553

ajax success function - check if response contains a string

Using PHP cURL to submit data to a remote server and jQuery ajax to do front end routine. All is fine except for the response data, the url I submit to returns some HTML after my flag set by echo in the PHP curl.

So what is returned looks like

Submission was successful<!DOCTYPE html>
//bunch of other html 

My success function is like this:

success: function(data) {
    if (result.indexOf("Submission was successful") > -1) {
    //Do stuff
    }

Also tried

if (data = "Submission was successful") {
//Do stuff
}

Which usually works when there is nothing after the "Submission was succesful". How do I get my success function to fire as long as the expected string is present, regardless of what comes after it?

Upvotes: 2

Views: 7394

Answers (2)

kei
kei

Reputation: 20511

Have you tried

if (data.indexOf("Submission was successful") > -1) {
  // Do stuff
}

Upvotes: 7

Samuel Cook
Samuel Cook

Reputation: 16828

success: function(data) {
    if (data.match(/submission was successful/i)) {
        //Do stuff
    }else{
        // failure
    }

}

Upvotes: 0

Related Questions