Home » Javascript » Email Live Checking PHP and Mysql

Email Live Checking PHP and Mysql

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have an issue im stuck for 3 days. Im trying to check in DB if the email the user is entering is already registered and avoid registering in duplicate. But this doesn’t seem to work fine

here is my code:

This is in the HTML

<script type="text/javascript">
   $(document).ready(function() {
    $("#cf_email").keyup(function(e) {
       var uname = $(this).val();
       if (uname == "")
       {
           $("#msg").html("");
           $("#Submit").attr("disabled", true);
       }
       else
       {
           $("#msg").html("Verificando, espere...");
           $.ajax({
               url: "check_availability.php",
               data: {Email: uname},
               type: "POST",
               success: function(data) {
                if(data.status == true) {
                    $("#msg").html('<span class="text-danger">Email ya registrado!</span>');
                    $("#Submit").attr("disabled", true);
                } else {
                    $("#msg").html('<span class="text-success">Email Disponible para Registrar!</span>');
                    $("#Submit").attr("disabled", false);
                }
            }
        });
    }
   });
  });
</script>

<right><form id="register" action="contact.php" method="post">
      <p><label>E-mail para Registro</label></p>
    <input type="text" name="cf_email" id="cf_email" title="Email"  class="demoInputBox" placeholder="Email Valido" required><div id="msg" class="form-group"></div>
</form></right>

this is the check_availability.php

$con = mysqli_connect($host, $user, $pass, $db) or die("Error " . mysqli_connect_error());

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if (isset($_POST["cf_email"]) && $_POST['cf_email'] != '')
{

   $response = array();
   $cfmail = mysqli_real_escape_string($con,$_POST['cf_email']);
   $sql  = "select Email from Bares where Email='".$cfmail."'";
   $res    = mysqli_query($con, $sql);
   $count  = mysqli_num_rows($res);
   if($count > 0)
   {
     $response['status'] = false;
     $response['msg'] = 'email already exists.';
   }
   else
   {
     $response['status'] = true;
     $response['msg'] = 'email is available.';
   }
   echo json_encode($response);

  }
?>

it doesn’t matter which email i introduce in the textfield because always says its available even if the email is registered already in the database

Answers:

In your JS you’re processing a string, not an object. You need to parse it:

success: function(data) {
    dataObj = JSON.parse(data);
    if(dataObj.status == true) {
         ....

Questions:
Answers:

This is an Example with mysqli OOP try it will work without Problem:
PHP:

<?php $mysqli=new mysqli($host, $user, $pass, $db); mysqli_set_charset($mysqli,'utf8');

if (!empty($_POST['cf_email'])){
    extract($_POST);
    $sql=$mysqli->query("select Email from Bares where Email='".$cf_email."'");
    if($sql->num_row>0){
        $status=1;
    }
    else{
        $status=0;
    }
    echo $status;
die;}
?>

HTML:

<right>
  <form id="register" action="contact.php" method="post">
    <p><label>E-mail para Registro</label></p>
    <input type="text" name="cf_email" id="cf_email" title="Email" class="demoInputBox" placeholder="Email Valido" required>
    <input type="submit" id="submit">
    <div id="msg" class="form-group"></div>
  </form>
</right>

JS:

<script type="text/javascript">
$(document).ready(function() {
  $("#cf_email").keyup(function(e) {
    var uname = $(this).val();
    if (uname == "") {
      $("#msg").html("");
      $("#submit").attr('disabled', true);
    } else {
      $.ajax({
        url: "check_availability.php",
        data: $(this).serialize(),
        type: "POST",
        success: function(data) {
          if (data == 1) {
            $("#msg").html('<span class="text-danger">Email ya registrado!</span>');
            $("#submit").attr('disabled', true);
          } else {
            $("#msg").html('<span class="text-success">Email Disponible para Registrar!</span>');
            $("#submit").attr('disabled', false);
          }
        }
      });
    }
  });
});
</script>