Reputation: 239
I would like to send this textfield value to a php file using jQuery.
HTML code:
<div>
<label for="email">Email: </label>
<input type="text" id="email" name="email" />
<span id="msgbox" class="msgbox"></span>
</div>
jQuery code:
$(document).ready(function()
{
$("#email").blur(function()
{
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
$.post("availability.php", { email: $(this).val() }, function(data)
{
if(data == 'yes')
{
$("#msgbox").fadeTo(200, 0.1, function()
{
$(this).html('Email Already exists').addClass('messageboxerror').fadeTo(900,1);
});
}
else
{
$("#msgbox").fadeTo(200, 0.1, function()
{
$(this).html('Email available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
PHP code:
include_once $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/magicquotes.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/db.inc.php';
$email = mysqli_real_escape_string($link, $_POST['email']);
$sql = "SELECT * FROM bradduser WHERE email='$email'";
$result = mysqli_query($link, $sql);
if(!$result)
{
$error = 'Error fetching email from bradduser.';
include 'error.html.php';
exit();
}
if(mysqli_num_rows($result) > 0)
{
echo ‘yes’; //email already exist
}
else
{
echo ‘no’;
}
The problem with the codes is that that doesn't seem to be a communication between jquery and php. The data refused to be sent to php. Please help to see what's wrong with the codes.
Upvotes: 0
Views: 276
Reputation: 29775
At first glance it looks correct, so a couple of things to look at:
Upvotes: 1
Reputation: 39724
echo ‘yes’;
is not a valid sintax
try:
echo 'yes';
with single quote
your jQuery scripting is ok but I think you have problems with availability.php
'
instead of ‘
availability.php
with the arguments, and to test use $_REQUEST['email']
then switch back. (eg: http://www.example.org/[email protected]
)Upvotes: 2