我希望验证电子邮件文本框,如果电子邮件已经存在,它应该在span中抛出错误,文本框应该为空,我使用JQuery完成了所有工作,但是无法清除字段,请帮助
这是我的电子邮箱
<tr>
<td>
<label> * Email</label>
</td>
<td>
<input type="text" id="email" name="email"/>
</td>
<td> <span id="email_status"></span></td>
</tr>
我对模糊事件的验证
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#email").blur(function() {
var email = $('#email').val();
if(email==="")
{
$("#email_status").html("");
}
else
{
$.ajax({
type: "POST",
cache: false,
url: "checkemail.php",
data: "email="+ email ,
success: function(html){
$("#email_status").html(html);
if (html == "Already exist"){
$("input:text[id=email]").val("");}
}
});
return false;
}
});
});
</script>
最后是php部分
<?php
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if(isset($_POST['email']))
{
$email=$_POST['email'];
$qu = "SELECT * FROM formdata WHERE email = '$email'";
$reslt=mysqli_query($conn,$qu);
if(mysqli_num_rows($reslt)>0)
{
echo "<span style='color:red;'>Already exist</span> ";
}
else
{
echo "<span style='color:green;'>Available</span>";
}
}
?>
答案 0 :(得分:1)
您需要从PHP文件发送true和false,并相应地在HTML文件中附加标记。
<?php
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if(isset($_POST['email']))
{
$email=$_POST['email'];
$qu = "SELECT * FROM formdata WHERE email = '$email'";
$reslt=mysqli_query($conn,$qu);
if(mysqli_num_rows($reslt)>0)
{
echo 0;
}
else
{
echo 1;
}
}
?>
这总是很好的做法。
尝试希望它有所帮助。
答案 1 :(得分:1)
<tr>
<td>
<label> * Email</label>
</td>
<td>
<input type="text" id="email" name="email"/>
</td>
<td> <span id="email_status"></span></td>
</tr>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#email").blur(function() {
var email = $('#email').val();
if(email=="")
{
$("#email_status").html("");
}
else
{
$.ajax({
type: "POST",
cache: false,
url: "checkemail.php",
data: "email="+ email ,
success: function(html){
$("#email_status").html(html);
if (html == "Already exist"){
$("input:text[id=email]").val("");}
}
});
return false;
}
});
});
</script>
<?php
$conn=mysqli_connect("localhost","root","your databse password","database name");
if(isset($_POST['email']))
{
$email=$_POST['email'];
$qu = "SELECT * FROM admin WHERE u_name = '$email'";
$reslt=mysqli_query($conn,$qu);
if(mysqli_num_rows($reslt)>0)
{
echo "<span style='color:red;'>Already exist</span> ";
}
else
{
echo "<span style='color:green;'>Available</span>";
}
}
?>
您的代码工作非常棒请检查您的DAtabase名称和数据库连接。你的java脚本代码正常工作 谢谢
答案 2 :(得分:1)
实际上你的js中的问题是你检查的if语句
if (html == "Already exist")
虽然来自php的返回数据是
<span style='color:red;'>Already exist</span>
我希望你明白这一点......所以
你的php应该
<?php
$conn=mysqli_connect("localhost","root","zaq12345","testdb");
if(isset($_POST['email']))
{
$email=$_POST['email'];
$qu = "SELECT * FROM formdata WHERE email = '$email'";
$reslt=mysqli_query($conn,$qu);
if(mysqli_num_rows($reslt)>0)
{
echo "Already exist";
}
else
{
echo "Available";
}
}
?>
在js中
success: function(html){
var html = html.trim();
if (html == "Already exist"){
$("#email_status").html("<span style='color:red;'>Already exist</span>");
$("#email").val("");
}else if (html == "Available"){
$("#email_status").html("<span style='color:green;'>Available</span>");
}
}
答案 3 :(得分:0)
您只需使用jQuery清除输入字段值即可。
$("form #email").val('');
答案 4 :(得分:0)
替换它:
$("input:text[id=email]").val("");
有了这个:
$("#email").val("");
答案 5 :(得分:0)
试试这个:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#email").blur(function() {
var email = $('#email').val();
if(email==="")
{
$("#email_status").html("");
}
else
{
$.ajax({
type: "POST",
cache: false,
url: "checkemail.php",
data: "email="+ email ,
success: function(html){
$("#email_status").html(html);
if (html == "Already exist"){
$("#email").val("");}//this was changed by me
}
});
return false;
}
});
});
</script>
答案 6 :(得分:0)
您确定'input:text[id=email]'
是正确的选择器吗?您可以致电console.log($('input:text[id=email]').length)
进行检查。