AGH that was annoying...After typing this whole thing I went to register with Google+ and now I have to type it all again...anyways...
I have a registration page on a CMS that I'm building. The first step in attempting to register is to enter an email address. Once it's entered, the email address is sent through ajax and checked against the database. If the email address isn't already used, ajax returns text back to the page saying, essentially "all good". If the email address IS already in use, ajax sends text back to the page, essentially saing "not so good".
Here's the page's code:
<script>
function checkEmail(str)
{
if(str == "") {
document.getElementById("emailresult").innerHTML = "";
return;
}
if(window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4) {
document.getElementById("emailresult").innerHTML = xmlhttp.responseText;
} else {
document.getElementById("emailresult").innerHTML = xmlhttp.status;
}
}
xmlhttp.open("GET","inc/ajax.php?checkemail=" + str, true);
xmlhttp.send();
}
</script>
<h3>Register for a new account</h3><br /><br />
<div style="border: 1px solid black; padding: 6px;">
<?php echo stripslashes($glo['reg_details']) ?>
</div><br /><br />
<form method="POST" action="">
<b>Please enter your Email Address</b><br />
<input type="email" name="emailaddress" size="45" required="required" placeholder="Enter a valid Email Address" onblur="checkEmail(this.value)" /><br /><br />
<div id="emailresult"></div><br /><hr /><br />
...and here's the processing page (just the stuff you need)...
if($_GET['checkemail']) {
$eml = $dba2->query_first("SELECT id FROM tbl_users WHERE email_address = '$_GET[checkemail]'");
if($dba2->affected_rows > 0) {
echo '<font style="color: red">This email address is already registered in the database.</font>';
} else {
echo '<font style="color: green">Email Address is Good!</font>';
}
}
Now what I have always been a bit lost on is how to run javascript IN ajax. What I want to do is this: When the query finds that the email address is unused "all good", to not only display the text, but to also open up the rest of the form for filling out (I haven't included the rest of the form above, but it'll be in a div with display="none"). This will require javascript to run, and for the likes of me I haven't found a good solution to this that works all the time. I've heard eval() is good, but I've also hear that it's bad...any help would be appreciated. Please include your suggestion in the code itself so I can see where it goes and how it functions. That way I'll remember it forever.