-1

I got this code from someone here from stack overflow I copied it and I tried it out but I doesn't work the way I want it to if I put an already existing email address from my database it gives an error saying email address already taken please choose another email. but when I submit an email address that is not in the database it still displays the same error how can this be fixed

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

and here is the php code

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

can someone help me solve this

4

5 回答 5

1

mysqli_num_rows is a function and therefore should be used as such — myseli_num_rows($select).

Also keep in mind that using $_POST['email'] in SQL query unescaped is a bad and dangerous practice.

And why mysqli_fetch_assoc() if you don't know whether you have any?

于 2013-10-27T16:15:26.627 回答
1

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

于 2013-10-27T16:17:22.923 回答
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();
            }
        });
    });
});
于 2013-10-27T16:20:18.500 回答
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"));
?>
于 2013-10-27T16:20:18.790 回答
0

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

据此, rows 应该是一个数组:

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

像这样的东西:

if (mysqli_num_rows($rows) > 0) {
  echo "exist";
}else echo 'notexist';
于 2013-10-27T16:23:44.953 回答