2012-07-26 127 views
4

我试图验证电子邮件地址是否已经存在于表中,但这不起作用。使用jQuery和PHP检查数据库中是否已经存在电子邮件地址

这里是我的代码:

<html><head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 
$('#Submit').click(function() { 
    var emailVal = $('#email').val(); // assuming this is a input text field 
    $.post('checkemail.php', {'email' : emailVal}, function(data) { 
     alert(data); 
    }); 
}); 
</script> 
</head> 
<body> 
<form id="form1" name="form1" method="post" action="view.php"> 
    <p> 
    <input type="text" name="email" id="email" /> 
    </p> 
    <p> 
    <input type="submit" name="Submit" id="Submit" value="Submit" /> 
    </p> 
</form> 
</body></html> 

当点击提交按钮,它应该首先确认使用jQuery和调用checkemail.php文件,如下所示:

<?php 
include("../includes/db.php"); 

$sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email']; 
$select = mysqli_query($con, $sql); 
$row = mysqli_fetch_assoc($select); 

if (mysqli_num_rows > 0) { 
    echo "The email already exists."; 
} 
?> 

然而,当我点击提交按钮它会去view.php而不是checkemail.php。在它重定向到view.php之前,它应该首先检查电子邮件是否已经存在。如果电子邮件已经存在,那么它不应该重定向到view.php。

回答

11
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ //newly added 
$('#Submit').click(function() {alert('in'); 
    var emailVal = $('#email').val(); // assuming this is a input text field 
    $.post('checkemail.php', {'email' : emailVal}, function(data) { 
     if(data=='exist') return false; 
     else $('#form1').submit(); 
    }); 
});}); 
</script> 
</head> 
<body> 
<form id="form1" name="form1" method="post" action="view.php"> 
    <p> 
    <input type="text" name="email" id="email" /> 
    </p> 
    <p> 
    <input type="button" name="Submit" id="Submit" value="Submit" /> 
    </p> 
</form> 
</body> 
</html> 

PHP代码

<?php 
include("../includes/db.php"); 

$sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email']; 
$select = mysqli_query($con, $sql); 
$row = mysqli_fetch_assoc($select); 

if (mysqli_num_rows > 0) { 
    echo "exist"; 
}else echo 'notexist'; 
?> 
+0

我想这一点,但仍然无法正常工作。即使我在表中已经存在的电子邮件字段中输入了电子邮件,它仍然会重定向到view.php。 – jaypabs 2012-07-26 07:24:28

+0

我的不好。此代码有效。问题是w /我的php代码。谢谢 – jaypabs 2012-07-26 07:32:37

+0

顺便说一句,我怎样才能在消息体内显示如:'

电子邮件已存在

'? – jaypabs 2012-07-26 07:34:48

0

我怀疑你必须中止提交按钮的标准操作。您也可能必须更改点击提交的操作,但现在无法对其进行测试。

$('#Submit').submit(function(e) { 
    var usernameVal = $('#email').val(); // assuming this is a input text field 
    $.post('checkemail.php', {'email' : emailVal}, function(data) { 
     alert('data'); 
    }); 
    e.preventDefault(); // <------ 
}); 
0

你可以做以下

  1. 在你的PHP文件 -

    if (mysqli_num_rows > 0) { 
         echo "0"; // email exists 
    else 
         echo "1"; // email doesn't exists 
    return; 
    
  2. 在HTML文件中,使用<input type="button"代替<input type="submit"

而且在这几样东西JavaScript的

$.post('checkemail.php', {'email' : emailVal}, function(data) { 
    if (data == 1) 
    { 
     $("#form1").submit(); 
    } 
    else 
    { 
     alert("Email already exists"); 
     return false; 
    } 
}); 
1

最佳做法是使用jQuery远程validate方法。

$("#myform").validate({ 
     rules: { 
     email: { 
      required: true, 
      email: true, 
      remote: "check-email.php" 
     } 
     }, 
     messages:{ 
     email:'Email address exists.' 
    } 
    }); 

在PHP文件中,你可以做@Sachyn提到的。

相关问题