PHP - Check Email Availability Using jQuery

Submitted by: 
Visitors have accessed this post 373 times.

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

And this is the link for the jquery that i used in this tutorial

Lastly, this is the link for the bootstrap that i used for the layout design

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.

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');
  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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Check Email Availability Using jQuery</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-6">
  20. <form>
  21. <div class="form-group">
  22. <label>Email</label>
  23. <input type="text" id="email" class="form-control"/>
  24. <div id="check_email"></div>
  25. </div>
  26. <div class="form-group">
  27. <label>Password</label>
  28. <input type="password" id="password" class="form-control"/>
  29. </div>
  30. <div class="form-group">
  31. <label>Firstname</label>
  32. <input type="text" id="firstname" class="form-control"/>
  33. </div>
  34. <div class="form-group">
  35. <label>Lastname</label>
  36. <input type="text" id="lastname" class="form-control"/>
  37. </div>
  38. <div id="result"></div>
  39. <div class="form-group">
  40. <center><button type="button" id="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save Data</button></center>
  41. </div>
  42. </form>
  43. </div>
  44. </div>
  45. </body>
  46. <script src="js/jquery-3.2.1.min.js"></script>
  47. <script src="js/script.js"></script>
  48. </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';
  4. $email = addslashes($_POST['email']);
  5. $password = $_POST['password'];
  6. $firstname = $_POST['firstname'];
  7. $lastname = $_POST['lastname'];
  9. $conn->query("INSERT INTO `member` VALUES('', '$email', '$password', '$firstname', '$lastname')");
  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.


  1. <?php
  2. require_once 'conn.php';
  3. $email = $_POST['email'];
  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. }
  16. }
  17. ?>

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;
  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. });
  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();
  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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.