keymistress
05-14-2007, 01:14 PM
hello, i wrote a script for registering users but there's an "error". with AJAX, when i enter a new e-mail address, it skips echo-ing "You have been registered" and goes straight to "This e-mail has already been registered" but the INSERT query did execute. help! :confused: i'm an AJAX noob so please bear with me. tried to put exit(); to the if statement but it didn't help. :(
if ($row[0] == 0)
{
//if email not in db, register user
$query2 = "INSERT INTO users (username, userPwd, userAccess) VALUES ('$email', '$userPwd', '$userAccess')";
echo $query2;
$result2 = mysql_query($query2);
echo "You have been registered";
}
else
{
//e-mail already registered
echo "This e-mail has already been registered";
}
}
BonRouge
05-14-2007, 02:01 PM
What do you have before that?
snake_eyes
05-14-2007, 02:08 PM
Provide the full code...
Regards
keymistress
05-14-2007, 06:28 PM
html form
<html>
<head>
<script src="userHint.js"></script>
</head>
<body>
<form>
Email: <input type="text" id="email" onkeyup="showHint(this.value)">
</form>
<p><div id="userHint"></div></p>
</body>
</html>
javascript
var xmlHttp
function showHint(email)
{
if (email.length==0)
{
document.getElementById("userHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getHint.php";
url=url+"?email="+email;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("userHint").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
same php but add $email=$_GET["email"]; and open and close php.
BonRouge
05-14-2007, 07:34 PM
Your php is as above but with '$email=$_GET["email"]' before it?!? So where does $row[0] come from then? It seems $row[0] is not defined, so $row[0] will never be equal to 0 (or anything else for that matter).
keymistress
05-14-2007, 07:44 PM
<?php
$email=$_GET["email"];
$query = "SELECT COUNT(*) FROM users WHERE username = '$email'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
//check if e-mail is already registered
if ($row[0] == 0)
{
//register user
//check student or staff
/*if (eregi("rp.sg$", $email))
{
$userAccess = 'Staff';
}
else
{
$userAccess = 'Student';
}
$query2 = "INSERT INTO users (username, userPwd, userAccess) VALUES ('$email', '$userPwd', '$userAccess')";
echo $query2;
$result2 = mysql_query($query2);*/
echo "An e-mail with your username and password has been sent to the address you have entered.";
}
else
{
//e-mail already registered
echo "This e-mail has already been registered. Forgot password?";
}
mysql_close($con);
}
else
{
echo ("E-mail is invalid. Please enter an RP e-mail address ending with 'rp.edu.sg' or 'rp.sg'.");
}
?>