使用 jQuery 和 PHP 检查数据库中是否已存在电子邮件地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11663867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:49:46  来源:igfitidea点击:

Check if email address already exists in the database using jQuery and PHP

phpjquerymysql

提问by jaypabs

I am attempting to validate an email address if it already exists in a table, but this isn't working.

如果某个电子邮件地址已存在于表中,我正在尝试验证该地址,但这不起作用。

Here's my code:

这是我的代码:

<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>

When the Submit button is clicked, it should validate first using the jQuery and call the checkemail.php file as shown below:

单击提交按钮时,应首先使用 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.";
}
?>

However, when I click the submit button it goes to view.php instead of checkemail.php. Before it redirects to view.php, it should check first if the email already exists or not. If the email already exists, then it should not redirect to view.php.

但是,当我单击提交按钮时,它转到 view.php 而不是 checkemail.php。在重定向到 view.php 之前,它应该首先检查电子邮件是否已经存在。如果电子邮件已经存在,则不应重定向到 view.php。

回答by naveen

<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 Code

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';
?>

回答by hsuk

Best practice for these kind of stuff is to use Jquery remote validate method.

这些东西的最佳实践是使用 Jquery 远程验证方法。

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

In PHP file, you can do what @Sachyn has mentioned.

在 PHP 文件中,您可以执行@Sachyn 提到的操作。

回答by Marcus Johansson

I suspect you have to abort the standard action of the submit button. You also might have to change action from click to submit, can't test it right now though.

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

$('#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(); // <------
});

回答by skos

You can do the following

您可以执行以下操作

  1. In your PHP file -

     if (mysqli_num_rows > 0) {
          echo "0"; // email exists
     else
          echo "1"; // email doesn't exists
     return;
    
  2. In your HTML file, use <input type="button"instead of <input type="submit"

  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"

And in the JavaScript

在 JavaScript 中

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