Reputation: 1920
After discussing this on SO Chat for hours, someone helped me get started and with a little additions by myself, the following JSFiddle was produced:
I have created a test mysql database on another online server. I have also placed a validation file with the following php code in it:
<?php
$db = new PDO('mysql:host=localhost;dbname=...', '...', '...');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$emailInput = $_POST['signUpemail'];
$result = $db->prepare('SELECT * FROM people WHERE email=?');
$result->execute(array($emailInput));
$count = count($result->fetchAll());
if ($count > 0){
echo 0;
}
else {
echo 1;
}
?>
The above file is called validation.php
and is placed on that same server. This file is accessed by the url
parameter in the JSFiddle AJAX code.
I have added action="google.com"
in the form declaration with the intent that the user is redirected to that url if the username entered is valid and the email entered is valid after being checked against my database.
The test email that already exists in the database is [email protected]
.
What am I doing wrong?
EDIT: Sorry, I didn't make clear what the problem was in the original post. The problem is that the email is not validating at all. No matter what email address I put in the textbox, regardless of whether its in the database or not, it doesn't go through. The page is not being redirected to google.com. The only reason I added google.com is to make it obvious whether the form is actually working or not.
Upvotes: 3
Views: 5540
Reputation: 6090
Your JavaScript code doesn't send any form parameters to your PHP script:
document.getElementById('signUpemail').onkeyup = function (e) {
$.ajax({
type: 'POST',
url: 'http://demcode.5gbfree.com/validate.php',
success: function (data) {
createError('Username is already registered');
}
});
}
Put some data into your arguments to $.ajax()
and see what happens.
Upvotes: 2