Results 1 to 4 of 4

Thread: AJAX responseText Evaluation problem

  1. #1
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default AJAX responseText Evaluation problem

    Hello all,

    I have been trying to code an AJAX function that checks if a username is unique, and alerts the user if it isn't. Basically the AJAX request calls a PHP page, passing in the username as a GET variable. If there username is already taken, the PHP text outputs a value, and if it's unique, it echoes nothing.

    I am trying to evaluate the response text of the AJAX object, but for some reason it never actually calls. Here is the AJAX function (the variable validUser is defined and initiated outside of the function, but is still in scope)

    Code:
    function checkUser(username)
    {
    	var xmlHttp;
    	try
    	{
    		xmlHttp=new XMLHttpRequest();
    	}
    	catch (e)
    	{
    		try
    		{
    			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    		}
    		catch (e)
    		{
    			try
    			{
    				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    			}
    			catch (e)
    			{
    				alert("Your browser does not support AJAX, or Javascript is turned off.");
    				return false;
    			}
    		}
    	}
    	  
    	xmlHttp.onreadystatechange=function()
    	{
    		if(xmlHttp.readyState == 4)
    			switch(xmlHttp.responseText)
    			{
    				case "returned":
    					validUser = 0;
    					break;
    				case "":
    					validUser = 1;
    					break;
    				default:
    					validUser = 1;
    					break;
    			}
    	}
    	xmlHttp.open("GET","functions/ajax/ajax_username.php?username=" + username,true);
    	xmlHttp.send(null);
    }
    Here is the PHP code:

    Code:
    <?php
    require_once("../../../functions/database.php");
    db_connect();
    
    $username = $_GET['username'];
    
    $result = mysql_query("SELECT * FROM `mulberry_users` WHERE `username` = '$username'");	
    
    
    if(mysql_num_rows($result) != 0)
    	echo "true";
    else
    	echo "false";
    
    ?>
    I would certainly welcome any help that you might be able to offer. For some reason however, calling the AJAX function, and then alerting the value of validUser simply returns "undefined".

    Any help most gratefully appreciated indeed.

    Thanks,
    Ben
    Last edited by benmajor; 08-18-2008 at 05:49 PM. Reason: Forgot to include PHP code.

  2. #2
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anyone?

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Based on the PHP file that you've provided the AJAX success case will execute the default case in the switch construct always. Your case labels like "returned" and "" will not work as you are returning true/false from the PHP side.

    Another possibility is the AJAX invokes the PHP part and it result in an error. In that case that error message will be send to the client and the responseText will hold that error message. In that case also it should execute the default case of the switch construct.

    I am trying to evaluate the response text of the AJAX object, but for some reason it never actually calls
    The above statement seems to be confusing what you are trying to do with the responseText? Are you trying to use eval()? If so it may not result correctly as eval() is supposed to execute JS code, not a non JS code. Moreover as per your PHP code you are returning either true or false to the client-side and they are not JS code and the eval() will fail.

    I've modified the code you've provided (commented the db part) and rest of the things worked as I've mentioned above.

    Hope this helps.

  4. #4
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hello there,

    Many thanks for your comments. I now realise what had caused the problem, especially now that you have pointed out to me that I erroneously returned different values to those being expected in the Javascript. So thanks on that one for starters.

    As for my statement about the evaluation of the responseText, this has also been resolved now, since it was being caused by the problems above.

    Many thanks for your time, sir!
    Ben

Bookmarks

Posting Permissions

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