错误电子邮件已存在

时间:2013-10-27 16:13:20

标签: javascript php email

我从堆栈溢出的人处获取此代码我复制了它并且我尝试了但是我没有按照我想要的方式工作如果我从我的数据库中放入一个已经存在的电子邮件地址它会给出错误说电子邮件地址已经采取请选择另一封电子邮件但是当我提交一个不在数据库中的电子邮件地址时,它仍会显示相同的错误,如何修复

<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("Email already exists. Please choose a different email");
var emailVal = $('#email').val(); // assuming this is a input text field

$.post('checkemail.php', {'email' : emailVal}, function(data) {
  if (data == 1)
  { 
     $("#registration").submit();
  }
 else
  {

      return false;
  }

 });
 });});
 </script>

  </head>
 <body>

 <form id="registration" name="registration" method="post" action="profile1.php">
 <p>email
 <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("con.php");

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

   if (mysqli_num_rows > 0) {
  echo "0"; // email exists
 }else{
  echo "1"; // email doesn't exists
 return;}
?>

有人可以帮我解决这个问题吗?

5 个答案:

答案 0 :(得分:1)

mysqli_num_rows是一个函数,因此应该这样使用 - myseli_num_rows($select)

另请注意,在$_POST['email']查询中使用SQL未转义是一种糟糕而危险的做法。

为什么mysqli_fetch_assoc()如果你不知道你有没有?

答案 1 :(得分:1)

问题是您在按下提交按钮时正在调用alert("Email already exists. Please choose a different email");。无论如何,这都会产生警报。

答案 2 :(得分:0)

$('#submit').click(function() {alert("Email already exists. Please choose a different email");

这将显示一条警告,其中包含每次单击提交按钮时已存在电子邮件的消息。您需要移动它,如下所示。

$(document).ready(function(){ //newly added 
    $('#submit').click(function() {
        var emailVal = $('#email').val(); // assuming this is a input text field

        $.post('checkemail.php', {'email' : emailVal}, function(data) {
            if(data=='exist') {
                alert("Email already exists. Please choose a different email");
                return false;
            } else {
                $('#registration').submit();
            }
        });
    });
});

答案 3 :(得分:0)

使用此

    <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() {

            var emailVal = $('#email').val(); // assuming this is a input text field
            $.post('checkemail.php', {'email' : emailVal}, function(data) {
                if(data.message == "exist")
                    alert("This email address already exists");
                else
                    $('#registration').submit();
            });

            return false;
        });
    </script>



        </head>
        <body>

         <form id="registration" name="registration" method="post" action="profile1.php">
         <p>email
         <input type="text" name="email" id="email" />
         </p>
         <p>
         <input type="submit" name="submit" id="submit" value="submit" />
         </p>
         </form>
        </body>
        </html>

在你的PHP代码中

<?php
include("con.php");

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

 if (mysqli_num_rows($select) > 0) {
   echo json_encode(array("message"=>"exist"));
 }
 else echo  echo json_encode(array("message"=>"notexist"));
?>

答案 4 :(得分:0)

看起来“mysqli_num_rows”是一个变量,可能不是fetch的结果。 $ row 的结果,所以也许你应该检查一下条件。

根据这个,行应该是一个数组:

http://php.net/manual/en/function.mysql-fetch-assoc.php

这样的事情:

if (mysqli_num_rows($rows) > 0) {
  echo "exist";
}else echo 'notexist';