admin管理员组文章数量:1321603
I am after a code for getting ajax response for checking whether an email address is already entered in the database. Here is my code snippet.
Java Script:
<script type="text/javascript">
pic1 = new Image(16, 16);
pic1.src="img/loader.gif";
$(document).ready(function(){
$("#email").change(function() {
var usr = $("#email").val();
if(usr.length >= 4)
{
$("#status").html('<img style="width:17px;" src="img/loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "check.php",
data: "email="+ usr,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){
if(msg == 'OK')
{
alert ("error");
$("#email").removeClass('object_error'); // if necessary
$("#email").addClass("object_ok");
$(this).html(' <img src="tick.gif" align="absmiddle">');
}
else
{
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
$(this).html(msg);
}
});
}
});
}
else
{
$("#status").html('<font color="red">' +
'Enter Valid Email</font>');
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
}
});
});
</script>
My Check.php file is as follows
<?php
// This is a sample code in case you wish to check the username from a mysql db table
if(isSet($_POST['email'])) {
$email = $_POST['email'];
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '';
$dbDatabase = 'jaquar_cdb';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword)
or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db)
or die ("Could not select database.");
$sql_check = mysql_query("select email from jaquar_contest where email='".$email."'")
or die(mysql_error());
if(mysql_num_rows($sql_check)) {
echo '<font color="red">The email <strong>'.$email.'</strong>'.
' is already in use.</font>';
} else {
echo 'OK';
}
}
?>
Finally the HTML code
<div class="sf_columns column_3">
<input id="email" type="text" placeholder="Email" name="email" data-required="true" data-email="true">
<div style="left:0px; padding-top: 10px;" id="status"></div>
</div>
The problem is that, I am not getting any response.. What is my mistake? Kindly help me..
I am after a code for getting ajax response for checking whether an email address is already entered in the database. Here is my code snippet.
Java Script:
<script type="text/javascript">
pic1 = new Image(16, 16);
pic1.src="img/loader.gif";
$(document).ready(function(){
$("#email").change(function() {
var usr = $("#email").val();
if(usr.length >= 4)
{
$("#status").html('<img style="width:17px;" src="img/loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "check.php",
data: "email="+ usr,
success: function(msg){
$("#status").ajaxComplete(function(event, request, settings){
if(msg == 'OK')
{
alert ("error");
$("#email").removeClass('object_error'); // if necessary
$("#email").addClass("object_ok");
$(this).html(' <img src="tick.gif" align="absmiddle">');
}
else
{
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
$(this).html(msg);
}
});
}
});
}
else
{
$("#status").html('<font color="red">' +
'Enter Valid Email</font>');
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
}
});
});
</script>
My Check.php file is as follows
<?php
// This is a sample code in case you wish to check the username from a mysql db table
if(isSet($_POST['email'])) {
$email = $_POST['email'];
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '';
$dbDatabase = 'jaquar_cdb';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword)
or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db)
or die ("Could not select database.");
$sql_check = mysql_query("select email from jaquar_contest where email='".$email."'")
or die(mysql_error());
if(mysql_num_rows($sql_check)) {
echo '<font color="red">The email <strong>'.$email.'</strong>'.
' is already in use.</font>';
} else {
echo 'OK';
}
}
?>
Finally the HTML code
<div class="sf_columns column_3">
<input id="email" type="text" placeholder="Email" name="email" data-required="true" data-email="true">
<div style="left:0px; padding-top: 10px;" id="status"></div>
</div>
The problem is that, I am not getting any response.. What is my mistake? Kindly help me..
Share Improve this question asked Oct 3, 2015 at 15:29 Jinson VargheseJinson Varghese 111 gold badge1 silver badge8 bronze badges 02 Answers
Reset to default 4The problem why not getting any response is here with AjaxComplete function
$("#status").ajaxComplete(function(event, request, settings)
I'm not sure why you are using it but you can do without it and infact even it's not necessary with your approach
without Ajaxplete function
made some changes in code.
pic1 = new Image(16, 16);
pic1.src="img/loader.gif";
$(document).ready(function(){
$("#email").change(function() {
var usr = $("#email").val();
if(usr.length >= 4){
$("#status").html('<img style="width:17px;" src="img/loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "check.php",
data: "email="+ usr,
dataType: 'text',
success: function(msg){
if(msg == 'OK'){
alert ("success");
$("#email").removeClass('object_error'); // if necessary
$("#email").addClass("object_ok");
$("#status").html(' <img src="tick.gif" align="absmiddle">');
} else {
alert ("error");
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
$("#status").html(msg);
}
}
});
} else {
$("#status").html('<font color="red">' + 'Enter Valid Email</font>');
$("#email").removeClass('object_ok'); // if necessary
$("#email").addClass("object_error");
}
});
});
SideNote: mysql_* functions is deprecated, so should stop using it and start using MySQLi
<?php
// This is a sample code in case you wish to check the username from a mysql db table
if(isset($_POST['email'])) { //change isSet to isset (it will not make any difference)
$email = mysql_real_escape_string($_POST['email']); //escape the string
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '';
$dbDatabase = 'jaquar_cdb';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword)
or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db)
or die ("Could not select database.");
$sql_check = mysql_query("SELECT email FROM jaquar_contest WHERE email='$email'") or die(mysql_error());
if(mysql_num_rows($sql_check) > 0) { //check rows greater then zero (although it will also not make any difference)
echo '<font color="red">The email <strong>'.$email.'</strong>'. ' is already in use.</font>';
} else {
echo 'OK';
}
}
?>
Change your first line of php code to :
if(isset($_POST['email']))
And so your updated Check.php file:
<?php
// This is a sample code in case you wish to check the username from a mysql db table
if(isset($_POST['email'])) {
$email = $_POST['email'];
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '';
$dbDatabase = 'jaquar_cdb';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword)
or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db)
or die ("Could not select database.");
$sql_check = mysql_query("select email from jaquar_contest where email='".$email."'")
or die(mysql_error());
if(mysql_num_rows($sql_check)) {
echo '<font color="red">The email <strong>'.$email.'</strong>'.
' is already in use.</font>';
} else {
echo 'OK';
}
}
?>
本文标签: javascriptHow to check Email already exist in db using ajaxStack Overflow
版权声明:本文标题:javascript - How to check Email already exist in db using ajax? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742103378a2420906.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论