Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax - Verifying if a row already exist in the database

I have a simple form for submitting an email address to a database. I want to check first, using javascript and AJAX, if that email address already exists in the table. The code I am using always returns true regardless if the row already exists in the database. Any hints?

JS Code

<script> 
$('form').submit(function(e) 
{
  alert('submit intercepted');
  e.preventDefault(e);
  var u = $('#email').val();
  $.ajax({
      url: './test2.php',
      type: 'POST',
      data: u,
      success: function (response) {
      //get response from your php page (what you echo or print)
        $('#status').append('<p>The email is ok to use and the row has been inserted </p><p>' + response);
        console.log('Submitted');
      },
      error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
        alert('Email exists');
      }
    });
});
</script>

HTML Code

<form id="form" action="./test2.php" method="post"> 
    E-mail: <input type="email" name="email" id="email" required><br>
    <span id="status">   </span>
    <input type="submit" id="btnsub">
  </form>

PHP Code

if(isset($email)) {

  $sql_email_check = $conn->query("SELECT Email FROM test WHERE Email='$email' LIMIT 1") ;

  $email_check = $sql_email_check->num_rows;

  if ($email_check < 1) {

    //return "Available";
    return true;   
    //header('Location: /test.php');
    exit();

  } 
  else 
  {
    //return "Unavailable";
    return false;
    //header('Location: /test.php');
    exit(); 

  }
}
like image 851
noel293 Avatar asked Sep 12 '17 08:09

noel293


People also ask

How can I check if a user exists in a database using AJAX?

Create ajaxfile. php file to handle AJAX requests. Check $_POST['username'] value exists in the username field in users table. If it exists then return Available.

What is AJAX have you used it if so tell the scenario?

AJAX stands for Asynchronous JavaScript and XML. It is used for allowing the client side of an application to communitcate with the server side of the application. Before AJAX, there was no way for the client side of a web application to communicate directly with the server. Instead, you would have to use page loads.

Does AJAX return a promise?

ajax returns a promise object, so that we don't have to pass a callback function.


1 Answers

So basically the problem was returning the answer from the php code to the ajax function. for some reason the return values were not interpreted correctly as they were expected (from my understanding anyway)

PHP CODE

if(isset($email)) {    
  $sql_email_check = $conn->query("SELECT Email FROM test WHERE Email='$email' LIMIT 1") ;     
  $email_check = $sql_email_check->num_rows;    

  if ($email_check == 0) {
    echo 'true';  
    return; 
    } 
  else 
  {
    echo 'false';
    return;    
  }
}

JS CODE

<script> 
$('form').submit(function(e) 
{
  alert('submit intercepted');
  e.preventDefault(e);
  var u = { 'email': $('#email').val() };
  $.ajax({
      url: 'test2.php',
      type: 'POST',
      //dataType: 'string',
      data: u,
      success: function (response) {
        //get response from your php page (what you echo or print)
        console.log(response);

        if (response === 'true') {
          $('#status').append('<p>The email is ok to use and the row has been inserted </p><p>' + response);

        } else {
          console.log('Email does exist ');

        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);

      }
    });
});
</script>
like image 144
noel293 Avatar answered Oct 12 '22 22:10

noel293