...

View Full Version : Jquery, parsing HTML AJAX response



bphein1980
07-13-2010, 03:15 AM
I'm make a ajax call to a processing PHP file that echos either "success" or an "error message".

When the success is returning, i want Jquery to recognize this and then redirect the browsers instead of displaying the success message.

Here are some snippets. I can only seem to parse to a blank or null.

Jquery code:

$.ajax({
type: "POST",
url: "process.php",
data: wfLoginPost(),
success: function(msg){

alert(msg);

// both below are either null or blank
//var res = $(msg).find("span.response").text();
//var res = $(msg).find("span.response").html();

if(res == 'Success'){
window.location = 'http://www.google.com';
}

}
});


process.php

if($qry[0]['pwd'] == $password){
echo "<span class='response'>Success</span>";
}else{
echo "<span class='response'>Username or password did not match</span>";
}

Here is the contents of the msg response from the ajax post:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<link rel="stylesheet" type="text/css" href="css/reset.css"/>
<link rel="stylesheet" type="text/css" href="css/960grid.css"/>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link rel="stylesheet" type="text/css" href="css/statusmessageQ.css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.statusmessageQ-0.5.js"></script>
</head>
<span class='response'>Success</span>

Kor
07-13-2010, 03:33 PM
Use a RegExp match():


if(res.match(/Success/g)){
window.location = 'http://www.google.com';
}

bphein1980
07-13-2010, 08:15 PM
Thanks. I will try that when I get home tonight. Why can I not get the contents of the <span> in the result msg using .text() or .html() though?

bphein1980
07-14-2010, 02:05 AM
.match() worked!

Thanks so much! :thumbsup:

Kor
07-14-2010, 10:30 AM
Why can I not get the contents of the <span> in the result msg using .text() or .html() though?
Never head about the text() nor html() methods.

match() is a method of the JavaScript Regular Expressions. More about RegExp in JavaScript:
http://lawrence.ecorp.net/inet/samples/regexp-intro.php

Gammerz
06-12-2011, 06:29 PM
Why can I not get the contents of the <span> in the result msg using .text() or .html() though?

I am a new to Jquery but I believe your code won't work because your HTML has not yet been added to your document's DOM.

To do add your Ajax response to an id called "placeholderID" within your document use:-

$("#placeholderID").html(msg);

If you give your success html an id of "success" and your failure response and id of "failure" then you can assign different classes (styles) according to the results of Ajax validation:-

$("#success").removeClass("failure").addClass("success");
$("#failure").removeClass("success").addClass("failure");

ohbigrighthand
06-13-2011, 04:53 AM
Never head about the text() nor html() methods.

those are both functions defined in jquery, html() returns the html markup for the selected element, text() returns the innerText



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum