PHP - Form Submit With Fade Effect Using jQuery

In this tutorial we will create a Form Submit With Fade Effect Using jQuery. This code can be use for searching some data from database, and some system that uses a large data. 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...

Before we get 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_fade, 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_fade');
  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. index.php
  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 - Form Submit With Fade Effect Using jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
  18. <br /><br />
  19. <div class="col-md-2"></div>
  20. <div class="col-md-8">
  21. <div class="panel panel-primary" id="login_form">
  22. <div class="panel-heading">Login</div>
  23. <div class="panel-body">
  24. <form>
  25. <div class="form-group">
  26. <label>Username</label>
  27. <input type="text" id="username" class="form-control"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Password</label>
  31. <input type="password" id="password" class="form-control"/>
  32. </div>
  33. <div id="status" style="display:none;"><center><label class="text-danger">Please complete the details...</label></center></div>
  34. <center><button type="button" id="login" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  35. </form>
  36. </div>
  37. </div>
  38. <div id="detail" style="display:none;"></div>
  39. </div>
  40. </div>
  41. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  42. <div class="modal-dialog" role="document">
  43. <form action="save.php" method="POST" enctype="multipart/form-data">
  44. <div class="modal-content">
  45. <div class="modal-body">
  46. <div class="col-md-2"></div>
  47. <div class="col-md-8">
  48. <div class="form-group">
  49. <label>Username</label>
  50. <input class="form-control" type="text" name="username"/>
  51. </div>
  52. <div class="form-group">
  53. <label>Password</label>
  54. <input class="form-control" type="password" name="password"/>
  55. </div>
  56. <div class="form-group">
  57. <label>Full Name</label>
  58. <input class="form-control" type="text" name="full_name" />
  59. </div>
  60. </div>
  61. </div>
  62. <div style="clear:both;"></div>
  63. <div class="modal-footer">
  64. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  65. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  66. </div>
  67. </div>
  68. </form>
  69. </div>
  70. </div>
  71. </body>
  72. <script src="js/jquery-3.2.1.min.js"></script>
  73. <script src="js/bootstrap.js"></script>
  74. <script src="js/script.js"></script>
  75. </html>
home.php
  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 - Form Submit With Fade Effect Using jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <?php
  18. require 'conn.php';
  19.  
  20.  
  21. $query = $conn->query("SELECT * FROM `member` WHERE `mem_id` = '$_SESSION[mem_id]'");
  22. $fetch = $query->fetch_array();
  23. ?>
  24. <h4>Welcome</h4>
  25. <center><h2><?php echo $fetch['name']?></h2></center>
  26. <a href="logout.php">Logout</a>
  27. </div>
  28. </body>
  29. </html>

Creating PHP Query

This code contains the php query of the application. This code will sent the data inputs to the database server, and then login the user account at the same time. To do that just copy and write this block of codes inside the text editor, then save it as shown below. save.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5.  
  6. if(!empty($_POST['username']) || !empty($_POST['password']) || !empty($_POST['full_name'])){
  7. $username = $_POST['username'];
  8. $password = $_POST['password'];
  9. $full_name = $_POST['full_name'];
  10.  
  11. $conn->query("INSERT INTO `member` VALUES('', '$username', '$password', '$full_name')");
  12.  
  13. echo "<script>alert('Data Inserted')</script>";
  14. echo "<script>window.location = 'index.php'</script>";
  15. }else{
  16. echo "<script>alert('Please complete the required field!')</script>";
  17. }
  18.  
  19. }
  20. ?>
login.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $username = $_POST['username'];
  5. $password = $_POST['password'];
  6.  
  7. $query = $conn->query("SELECT * FROM `member` WHERE `username` = '$username' && `password` = '$password'");
  8. $row = $query->num_rows;
  9. $fetch = $query->fetch_array();
  10. if($row > 0){
  11. echo "success";
  12. $_SESSION['mem_id'] = $fetch['mem_id'];
  13. }else{
  14. echo "error";
  15. }
  16. ?>
logout.php
  1. <?php
  2. header('location: index.php');
  3. ?>

Creating jQuery Script

This is where the main function of the application. This code will apply some fade effect to a specific div when the ajax request has been send. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2. var error = $(
  3. '<center><h3>Invalid username or password</h3></center>'
  4. + '<center>Going back to login..</center>'
  5. + '<center><img src="ico/loading.gif" width="100px"/></center>'
  6. );
  7.  
  8. var success = $(
  9. '<center><h3>Login successful</h3></center>'
  10. + '<center>Logging in to homepage..</center>'
  11. + '<center><img src="ico/loading.gif" width="100px"/></center>'
  12. );
  13.  
  14.  
  15.  
  16. $('#login').on('click', function(){
  17. var username = $('#username').val();
  18. var password = $('#password').val();
  19.  
  20. if(username != "" || password != ""){
  21. $.ajax({
  22. url: 'login.php',
  23. type: 'POST',
  24. data: {username: username, password: password},
  25. success: function(data){
  26. $('#login_form').hide();
  27. if(data == 'success'){
  28. $('#detail').attr('class', 'alert alert-success')
  29. success.appendTo($('#detail'));
  30. $('#detail').fadeIn(1000);
  31. setTimeout(function(){
  32. window.location = 'home.php';
  33. }, 4000);
  34. }else if(data == "error"){
  35. $('#detail').attr('class', 'alert alert-danger')
  36. error.appendTo($('#detail'));
  37. $('#detail').fadeIn(1000);
  38. setTimeout(function(){
  39. $('#detail').hide();
  40. $('#login_form').fadeIn(1000);
  41. }, 4000);
  42. }
  43.  
  44. $('#username').val('');
  45. $('#password').val('');
  46. }
  47. });
  48.  
  49. }else{
  50. $('#login').attr('disabled', 'disabled');
  51. $('#status').show();
  52. setTimeout(function(){
  53. $('#login').removeAttr("disabled");
  54. }, 2000);
  55.  
  56. }
  57. });
  58. });
There you have it we successfully created Form Submit With Fade Effect 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