PHP - Check Email Availability Using jQuery

In this tutorial we will create a Check Email Availability Using jQuery. By using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_email, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_email');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as shown below.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Check Email Availability Using jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-6">
  19. <form>
  20. <div class="form-group">
  21. <label>Email</label>
  22. <input type="text" id="email" class="form-control"/>
  23. <div id="check_email"></div>
  24. </div>
  25. <div class="form-group">
  26. <label>Password</label>
  27. <input type="password" id="password" class="form-control"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Firstname</label>
  31. <input type="text" id="firstname" class="form-control"/>
  32. </div>
  33. <div class="form-group">
  34. <label>Lastname</label>
  35. <input type="text" id="lastname" class="form-control"/>
  36. </div>
  37. <div id="result"></div>
  38. <div class="form-group">
  39. <center><button type="button" id="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save Data</button></center>
  40. </div>
  41. </form>
  42. </div>
  43. </div>
  44. </body>
  45. <script src="js/jquery-3.2.1.min.js"></script>
  46. <script src="js/script.js"></script>
  47. </html>

Creating Save Query

This code contains the saving query for the application. This will store the data inputs to the database server after completing all the required fields. To create this just write this code inside the text editor then save it as save_query.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $email = addslashes($_POST['email']);
  5. $password = $_POST['password'];
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  8.  
  9. $conn->query("INSERT INTO `member` VALUES('', '$email', '$password', '$firstname', '$lastname')");
  10.  
  11. ?>

Creating Email Validation

This code contains the main function of the application. This will validate every input of the user in the text box to check if is available to use. To make these just kindly follow and write these block of codes inside the text editor then is show below. validation.php
  1. <?php
  2. require_once 'conn.php';
  3. $email = $_POST['email'];
  4.  
  5. if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^",$email)){
  6. echo "error";
  7. }else{
  8. $query = $conn->query("SELECT * FROM `member` WHERE `email` = '$email'");
  9. $rows = $query->num_rows;
  10. if($rows > 0){
  11. echo "invalid";
  12. }else{
  13. echo "success";
  14. }
  15.  
  16. }
  17. ?>
script.js Note: To make this script works make sure you already import the jquery plugin to these application.
  1. $(document).ready(function(){
  2. var email_status;
  3.  
  4. $('#email').on('keyup', function(){
  5. $.post(
  6. 'validation.php',
  7. {email: $(this).val()},
  8. function(data){
  9. if(data == "success"){
  10. $('#check_email').html("<label class='text-success'>Email is valid</label>")
  11. email_status = true;
  12. }else if(data == "error"){
  13. $('#check_email').html("<label class='text-danger'>This is not a valid email</label>")
  14. email_status = false;
  15. }else if(data == "invalid"){
  16. $('#check_email').html("<label class='text-danger'>Email is already taken!</label>")
  17. email_status = false;
  18. }
  19. });
  20. });
  21.  
  22. $('#save').on('click', function(){
  23. var email = $('#email').val();
  24. var password = $('#password').val();
  25. var firstname = $('#firstname').val();
  26. var lastname = $('#lastname').val();
  27.  
  28. if(email_status === false || password == "" || firstname == "" || lastname == ""){
  29. $('#result').html("<center><label class='text-warning'>Please fill up the required field!</label></center>")
  30. }else{
  31. $.post(
  32. 'save_query.php',
  33. {email: email, password: password, firstname: firstname, lastname: lastname},
  34. function(){
  35. $('#email').val('');
  36. $('#password').val('');
  37. $('#firstname').val('');
  38. $('#latname').val('')
  39. $('#result').html("<center><label class='text-success'>Data Inserted</label></center>")
  40. });
  41. }
  42. });
  43. });
There you have it we successfully created Check Email Availability Using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment