Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

javascript - Ajax email Availability check with php not working

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: 'includesemailAvailability.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 file


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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

This Jquery :

 $(document).ready(function(){
    $('#myform').submit(function(event){
    event.preventDefault();
    var formValues = $(this).serialize();
        $.ajax({
        url:"includesemailAvailability.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();
                }
            }
        });
    });
});

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...