I'm using Ajax to check is if the fields in my registation form is valid or not.
My validate function:
<script type='text/javascript'>
function validate(field, query) {
xmlhttp = new XMLHttpRequest(); // Creating Object
xmlhttp.onreadystatechange = function () // Checking if readyState changes
{
if (xmlhttp.readyState != 4 && xmlhttp.status == 200) // Validation Under Process
{
document.getElementById(field).innerHTML = "Validating..";
}
else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) // Validation Completed
{
document.getElementById(field).innerHTML = xmlhttp.responseText;
}
else // If an error is encountered
{
document.getElementById(field).innerHTML = "Unknown Error Occurred. <a href='index.php'>Reload</a> the page.";
}
}
xmlhttp.open("GET", "check.php?field=" + field + "&query=" + query, false);
xmlhttp.send();
}
</script>
And my check.php:
if ($field == "email")
{
$check = $db->prepare("SELECT * FROM users WHERE email = :query");
$check->bindParam(':query', $query);
$check->execute();
$count = $check->rowCount();
if ($count > 0)
{
echo "<script type='text/javascript'>document.getElementById('regr_btn').disabled = true;</script>";
echo "<font color=red>Email already exists</font>";
}
else
{
echo "<script type='text/javascript'>document.getElementById('regr_btn').disabled = false;</script>";
echo "<font color=green>Email available</font>";
}
if (!filter_var($query, FILTER_VALIDATE_EMAIL)) {
echo "<font color=red><br />Please enter a valid Email</font>";
echo "<script type='text/javascript'>document.getElementById('regr_btn').disabled = true;</script>";
}
else {
echo "<script type='text/javascript'>document.getElementById('regr_btn').disabled = false;</script>";
My echo "<font color=red>Email already exists</font>";
work completely fine.
But as you can see, I also try to echo "<script type='text/javascript'>document.getElementById('regr_btn').disabled = false;</script>";
when an email already exist in my database.
But somehow it doesn't work. I also tried to display:none instead of disabeling my button, but still with no result.
If i put document.getElementById('regr_btn').disabled = true;
into the console of google chrome, then it works fine. So somehow my javascript isn't called.
I hope that you can help me out.