使用php的Ajax电子邮件可用性检查不起作用 [英] Ajax email Availability check with php not working

查看:111
本文介绍了使用php的Ajax电子邮件可用性检查不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查电子邮件的可用性,但是无法正常工作.而且我是javascript和ajax的新手,请帮助我.

I want to check email availability but it's not working. and also I am new to javascript and ajax please help me.

他是我的代码 具有跨度的电子邮件输入以显示输出(目前没有输出)

his is my code email input with span to show output(for now there is no output)

<input class="input--style-4" id="email" type="email" name="email" required>
<span id="user-availability-status"></span>

JS

<script>
  $(document).ready(function() {
    $('#email').blur(function() {
      var email = $(this).val();

      $.ajax({
        url: 'includes\emailAvailability.php',
        method: "POST",
        data: {
          email_val: email
        },
        success: function(data) {
          if (data != 0) {
            $('#user-availability-status').html('<span>Username blah not available</span>');
            $('#register').attr("disabled", true);
          } else {
            $('#user-availability-status').html('<span>Username blah Available</span>');
            $('#register').attr("disabled", false);
          }
        }
      })
    });
  });
</script>

PHP文件


<?php

if (isset($_POST["email_val"])) {
    include("DbConn.php");
    $email = mysqli_real_escape_string($conn, $_POST["email_val"]);
    $query = "SELECT * FROM customer WHERE email = '" . $email . "'";
    $result = mysqli_query($conn, $query);
    echo mysqli_num_rows($result);
}

推荐答案

您应该检查我在评论中引用的链接,它是您的完整答案.

You should check link I refered in comment its your complete answer.

这是您的代码的简单示例.

here is a Simple example with your code.

include("DbConn.php");
    // Set alerts as array 
$error     = "";

    // I should just trrim and let you check if email is empty .lol 
if (empty($_POST["email_val"])) {
    $error .= "<p class='error'>Fill email value.</p>";

    //Check if this is a real email 
} elseif(!filter_var($_POST["email_val"],FILTER_VALIDATE_EMAIL)){
    $error .= "<p class='error'>Wrong email type.</p>";
}else{
    $email = mysqli_real_escape_string($conn, $_POST["email_val"]);

    //You should use prepare statement $email, Shame on you .lol    
    $query = "SELECT * FROM customer WHERE email = '{$email}'");
    $result = mysqli_query($conn, $query);
    echo mysqli_num_rows($result);
    $error .= "ok";
}
$data = array(
 'error'  => $error
);

此Jquery:

 $(document).ready(function(){
    $('#myform').submit(function(event){
    event.preventDefault();
    var formValues = $(this).serialize();
        $.ajax({
        url:"includes\emailAvailability.php",
        method:"POST",
        data:formValues,
        dataType:"JSON",
            success:function(data){
                if(data.error === 'ok'){
                    $('#result').html(data.error);
                } else {
                    $('#result').html(data.error);
                    $('#myform')[0].reset();
                }
            }
        });
    });
});

和Html:

<form id="myform">
  <input class="input--style-4" id="email" type="email" name="email_val">
  <span id="result"></span>
  <button type="button" class="btn btn-primary">Send</button>
</form>

这篇关于使用php的Ajax电子邮件可用性检查不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆