0

if I input an email that is not in the database, always the same alert with the alert if the email is in the database. What's wrong with my coding ?

enter image description here

I input email '[email protected]' and I enter the result instead 'Email Address can used' should 'Email Address Already in Use'.

 <html> <head> <!-- sweet alert --> <link rel="stylesheet" type="text/css" href="css/sweetalert.css"> <script type="text/javascript" src="js/sweetalert.min.js"></script> <!-- end sweet alert --> <script type="text/javascript"> $(document).ready(function(){ $('#email').blur(function(){ var email = $(this).val(); $.ajax({ type : 'POST', url : 'check-email.php', data : 'email='+email, success : function(data){ if(data==0) { swal({ title: "Email Address can used", text: "", type: "success" }); } else { swal({ title: "Email Address Already in Use", text: "", type: "warning" }); } }, }); }); }); </script> </head> <body> <form class="form-horizontal" method="POST" name="form"> <input type="email" name="email" id="email" class="form-control" required> </form> </body> </html> 

check-email.php

<?php include 'libraries/config.php'; $email = $_POST['email']; $cekdata=mysqli_query($conn,"SELECT * FROM user_csr WHERE email = '$email'"); ?> 

result : enter image description here

2
  • are you getting any error? Commented Jan 19, 2018 at 4:34
  • What do you see in the console when after console.log(data) ? Commented Jan 19, 2018 at 4:45

3 Answers 3

3

You need to send back the data to the ajax using echo or print (I use die() in this case). I also tend to use json to respond. You should check that the email is valid at the very least but you should be binding the email value instead of injecting it into the sql string:

PHP:

<?php include 'libraries/config.php'; # Set a default response $def = ['alert' => true]; # Remove empty values $email = trim($_POST['email']); # First check this is an actual email if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { die(json_encode($def)); } # You should bind/prepare $email, not insert variable into string $stmt = $conn->prepare("SELECT COUNT(*) as count FROM `user_csr` WHERE `email` = ?"); $stmt->bind_param('s', $email); $stmt->execute(); $result = $stmt->get_result(); # Fetch the results of the count $result = $result->fetch_assoc(); # Write json response die(json_encode([ # If succeeded, write the count 'counted' => $result ? $result['count'] : 0 ])); 

JavaScript:

<script type="text/javascript"> $(document).ready(function(){ $('#email').blur(function(){ var email = $(this).val(); $.ajax({ type: 'POST', url: 'check-email.php', data: 'email='+email, success: function(response){ // Parse response response = JSON.parse(response); // See if alert is set (email is not valid) if(typeof response.alert !== "undefined") { // Set an program alert alert('A program error occurred.'); // Stop return false; } var counted = response.counted; swal({ title: (counted == 1)? "Email Address Already in Use" : "Email Address can used", text: "", type: (counted == 1)? "warning" : "success" }); }, }); }); }); </script> 
Sign up to request clarification or add additional context in comments.

Comments

0

As your program scenario, just swap the if body in success function, here code snippet

 success : function(data){ if(data==0) { swal({ title: "Email Address Already in Use", text: "", type: "warning" }); } else { swal({ title: "Email Address can used", text: "", type: "success" }); } }, 

Comments

-1

Please review this code where i have some PHP code and JS code as well

<?php include 'libraries/config.php'; $email = isset($_POST['email'])?$_POST['email']:''; if(!empty($email)){ $cekdata=mysqli_query($conn,"SELECT * FROM user_csr WHERE email = '$email'"); return $cekdata->num_rows; // if email found it returns 1 else 0 }else{ return 0; } 

And Finally you need to check the return value in response

<script type="text/javascript"> $(document).ready(function () { $('#email').blur(function () { var email = $(this).val(); if(email==""){ alert('Please enter email address'); return false; } $.ajax({ type: 'POST', url: 'check-email.php', data: 'email=' + email, success: function (data) { if (data == 0) { swal({ title: "Email Address can use", text: "", type: "success" }); } else { swal({ title: "Email Already Exists", text: "", type: "warning" }); } }, }); }); }); </script> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.