benmajor
08-18-2008, 05:46 PM
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)
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:
<?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
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)
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:
<?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