Submit PHP Bootstrap Form without Page Refresh using jQuery, Ajax

Submit PHP Bootstrap Form without Page Refresh using jQuery, Ajax

Submit PHP Bootstrap Form into MySQL table without Page Refresh using jQuery, Ajax

AJAX (Asynchronous JavaScript and XML) is known as developer's dream because while working with ajax, you do not need to reload page for update any module.

In this tutorial, i am going to tell you how to submit a simple php form without page refresh using jquery ajax and after submitting it will display you all records from database into html table.

Here, I have created a responsive bootstrap form which will take some inputs from you such as name,email and your contact no and insert these details into users table.

To insert into database table, i create a PDO object representing a connection to a database.

Simple HTML Form : ajaxsubmit.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Submit PHP Form without Page Refresh using jQuery, Ajax</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  6. <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <div class="row">
  11. <div class="col-md-offset-3 col-md-6">
  12.     <div class="panel panel-primary">
  13. <div class="panel-heading">Submit PHP Form without Page Refresh using jQuery, Ajax</div>
  14. <div class="panel-body">
  15. <div id="form-container">
  16.          <form method="post" id="signup-form" autocomplete="off">
  17.                         
  18.                 <div class="form-group">
  19.                 <input type="text" class="form-control" name="name" id="name" placeholder="Name" required />
  20.                 </div>
  21.                             
  22.                 <div class="form-group">
  23.                 <input type="email" class="form-control" name="email" id="email" placeholder="Your Mail" required />
  24.                 </div>
  25.                             
  26.                 <div class="form-group">
  27.                 <input type="text" class="form-control" name="mobile" id="mobile" placeholder="Contact No" required />
  28.                 </div>
  29.                             
  30.                 <hr />
  31.                             
  32.                 <div class="form-group">
  33.                 <button class="btn btn-primary">Submit</button>
  34.                 </div>
  35.                         
  36.          </form>
  37.          </div>
  38.         </div>
  39.      </div>
  40.     </div>
  41. </div>
  42. </body>
  43. </html>
Ajax script to submit form

$.ajax() is a best way to pass form data to server.

In this script, i am sending form data using serialize() method to ajaxsubmit.php with HTTP POST Request.

  1. <script>
  2. $('#signup-form').submit(function(e){
  3. e.preventDefault(); // Prevent Default Submission
  4. $.ajax({
  5.     url: 'ajaxsubmit.php',
  6.     type: 'POST',
  7.     data: $(this).serialize(), // it will serialize the form data
  8. dataType: 'html'
  9. })
  10. .done(function(data){
  11.      $('#form-container').fadeOut('slow', function(){
  12.      $('#form-container').fadeIn('slow').html(data);
  13. });
  14. })
  15. .fail(function(){
  16.         alert('Ajax Submit Failed ...');    
  17. });
  18. });
  19. </script>
ajaxsubmit.php

In this step, i create connection using PDO object and then insert form data into users table and then i list down all records of users table into tables.

  1. <?php
  2. $host = "localhost";
  3. $user = "root";
  4. $pass = "admin";
  5. $dbname = "testdba";
  6. try{
  7. $con = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
  8. $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. }catch(PDOException $ex){
  10. die($ex->getMessage());
  11. }
  12. $name=$_POST['name'];
  13. $email=$_POST['email'];
  14. $mobile=$_POST['mobile'];
  15. $query = "insert into users(name, email, mobile) values ('$name', '$email', '$mobile')";
  16. $stmt = $con->prepare( $query );
  17. $stmt->execute();
  18. ?>
  19. <table class="table table-bordered table-condensed table-hover table-striped">
  20. <tr>
  21.      <td colspan="3">
  22.          <div class="alert alert-info">
  23.              <strong>Success</strong>, Form Submitted Successfully...
  24.          </div>
  25.      </td>
  26. </tr>
  27. <tr>
  28.     <th>#ID</th>
  29. <th>Name</th>
  30. <th>Email</th>
  31. </tr>
  32. <?php
  33. $query = "SELECT id, name, email FROM users";
  34. $stmt = $con->prepare( $query );
  35. $stmt->execute();
  36. while ($row=$stmt->fetch(PDO::FETCH_ASSOC) ) {
  37. extract($row);
  38. ?>
  39. <tr>
  40. <td><?php echo $id; ?></td>
  41. <td><?php echo $name; ?></td>
  42. <td><?php echo $email; ?></td>
  43. </tr>
  44. <?php
  45. }
  46. ?>
  47. </table>

I suggest you to create seperate config file for database connection settings.

Database Design & Table

In this step, First create database if not exist then create table manually or you can create tables by running following MySQL query:

  1. CREATE DATABASE testdba;
  2. ----------------------------------------------------------
  3. CREATE TABLE IF NOT EXISTS users(
  4. id int(10) NOT NULL AUTO_INCREMENT,
  5. name varchar(255) NOT NULL,
  6. email varchar(255) NOT NULL,
  7. mobile varchar(20) NOT NULL,
  8. PRIMARY KEY (id)
  9. )
  10. ----------------------------------------------------------

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort karapürçek escort karasu escort kaynarca escort kocaali escort otele gelen escort pamukova escort sapanca escort söğütlü escort taraklı escort
sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort izmit escort sakarya escort sapanca escort sakarya escort