I'm trying to post a field value to php, check in my db to see if it exists, then return and handle on the form according with a pop up saying what to do. I do also have it backwards, as the registration is invite only so you'll notice in the php that you do in fact have to have your email there.
It returns "empty" and handles it exactly fine when there is nothing in the field. If I put anything at all in the input though it gives the error and I have no idea why...
java part, and like it said, it works when its 'empty'
PHP Code:
$('form.register_form input[name=email]').bind("oninput", function ()
{
var messageBox = $('form.register_form span#msgbox2');
var input = $(this);
messageBox.removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
var getData = $.post('process_availability.php', { email:input.val() });
getData.done(function(data) {
alert(data);
//if correct login detail
if (data == 'empty')
{
// blank input
messageBox.fadeTo(200,0.1,function() {
//add message and change the class of the box and start fading
messageBox.text('Please put in a valid email!').addClass('messageboxerror').fadeTo(900,1);
});
alert('Please put in a valid email!');
}
if (data == 'yes')
{
// email is available
messageBox.fadeTo(200,0.1,function() {
//add message and change the class of the box and start fading
messageBox.text('This email is available to register!').addClass('messageboxok').fadeTo(900,1);
});
alert('This email is available to register!');
}
if (data == 'no')
{
// email is not in database
messageBox.fadeTo(200,0.1,function() {
//add message and change the class of the box and start fading
messageBox.text('This email could not be found in our database!').addClass('messageboxerror').fadeTo(900,1);
});
alert('This email could not be found in our database!');
}
if (data == 'already')
{
// email has already been registered
messageBox.fadeTo(200,0.1,function() {
//add message and change the class of the box and start fading
messageBox.text('This email is already registered!').addClass('messageboxerror').fadeTo(900,1);
});
alert('This email is already registered!');
}
});
//.fail(function(jqXHR, textStatus, errorThrown) { alert(textStatus+ ': ' +errorThrown); }).always(function() { alert("finished"); });
});
the php to check
PHP Code:
<?php
//check email availability
if ($_POST['email'] != null) {
//require necessary files
require('config/dbconfig.php');
include('classes/class.mysqli.php');
//change post to variable
$email = $_POST['email'];
//set up bind for query
$bind = array(
":email_bind" => "$email"
);
//query email to see if it is in database
$results = $db->select('users','email=:email_bind',$bind,'*','','');
//use boolean to check results
$trueFalse = (count($results) > 0) ? 0 : 1;
if ($trueFalse == 0)
{
foreach($results as $row)
{
if ($row['regcode'] == null)
{
echo 'already';
}else
{
echo 'yes';
}
exit;
}
} else {
//email is blank
echo 'no';
exit;
}
}else
{
echo 'empty';
}
?>