Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Jquery, parsing HTML AJAX response

    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:
    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
    PHP Code:
        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:

    Code:
    <!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>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Use a RegExp match():
    Code:
    if(res.match(/Success/g)){
    	window.location = 'http://www.google.com';
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts
    .match() worked!

    Thanks so much!

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by bphein1980 View Post
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bphein1980 View Post
    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");

  • #7
    Regular Coder
    Join Date
    Apr 2010
    Posts
    103
    Thanks
    2
    Thanked 15 Times in 15 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •