• We just launched and are currently in beta. Join us as we build and grow the community.

How to Check Email Availability using jQuery and PHP/MySQLi

evilangel

Exploitation Chain Developer
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
152
Likes
12
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Getting Started

First, we need the jQuery library and also Bootstrap for a better design to our app. I've included these files in the downloadable of this tutorial but if you want, you can download them yourself using the links below:

For Bootstrap
For jQuery
Creating our Database

Next, we create the database that we are going to filter in this tutorial.

I've included a SQL file in the downloadable of this tutorial. All you have to do is import the said file. If you have no idea on how to import, please visit my tutorial How import .sql file to restore MySQL database.

You should be able to create a database named mydb.

Creating our Form

Next, we create our form or input text for the email. Also, I've displayed the available emails in our database for our reference. Create a new file, name it as index.php and paste the codes below.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>How to Check Email Availability using jQuery and PHP/MySQLi</title>
  6. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">Check Email Availability using jQuery</h1>
  11. <div class="row">
  12. <div class="col-sm-4 col-sm-offset-2">
  13. <div class="form-group">
  14. <label>Email</label>
  15. <input type="text" class="form-control" name="email" id="email">
  16. <span id="response" style="display:none;"></span>
  17. </div>
  18. </div>
  19. <div class="col-sm-4">
  20. <table class="table table-bordered">
  21. <thead>
  22. <th>Email</th>
  23. </thead>
  24. <tbody>
  25. <?php
  26. //connection
  27. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'mydb'

    )

    ;

  28. $sql

    =

    "SELECT * FROM users"

    ;
  29. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  30. while

    (

    $row

    =

    $query

    ->

    fetch_assoc

    (

    )

    )

    {
  31. echo

    "
  32. <tr>
  33. <td>"

    .

    $row

    [

    'email'

    ]

    .

    "</td>
  34. </tr>
  35. "

    ;
  36. }
  37. ?>
  38. </tbody>
  39. </table>
  40. </div>
  41. </div>
  42. </div>
  43. <script src="jquery.min.js"></script>
  44. <script type="text/javascript">
  45. $(document).ready(function(){
  46. $('#email').keyup(function(e){
  47. e.preventDefault();
  48. var email = $(this).val();
  49. $.ajax({
  50. method: 'POST',
  51. url: 'check.php',
  52. data: {keyword: email},
  53. dataType: 'json',
  54. success: function(response){
  55. $('#response').show().html(response);
  56. }
  57. });
  58. });
  59. });
  60. </script>
  61. </body>
  62. </html>

In here, we have setup that upon user keyup, we generate a response.

Creating our Check Script

Lastly, we create the script that checks the availability of the inputted email. Create a new file, name it as check.php and paste the codes below.

  1. <?php
  2. //initailize output
  3. $output

    =

    ''

    ;

  4. //connection
  5. $conn

    =

    new

    mysqli(

    'localhost'

    ,

    'root'

    ,

    ''

    ,

    'mydb'

    )

    ;

  6. //input post
  7. $keyword

    =

    $_POST

    [

    'keyword'

    ]

    ;

  8. //server side email validation
  9. if

    (

    !

    filter_var

    (

    $keyword

    ,

    FILTER_VALIDATE_EMAIL)

    )

    {
  10. $output

    =

    'Invalid email format'

    ;
  11. }
  12. else

    {
  13. //check if email exist
  14. $sql

    =

    "SELECT * FROM users WHERE email = '$keyword

    '"

    ;
  15. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  16. if

    (

    $query

    ->

    num_rows

    >

    0

    )

    {
  17. $output

    =

    'Email already taken'

    ;
  18. }
  19. else

    {
  20. $output

    =

    'Email is free to use'

    ;
  21. }
  22. }

  23. echo

    json_encode

    (

    $output

    )

    ;

  24. ?>

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top