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
okey metin2 pvp serverler metin2 pvp
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat

viagra fiyatı

viagra 100 mg fiyat

viagra sipariş

orjinal viagra

viagra satın al

izmir escort
ataşehir escort
kocaeli escort izmit escort bayan gebze escort izmit sınırsız escort escort bursa tuzla escort aydınlı escort orhanlı escort bursa escort escort bayan istanbul escort istanbul sınırsız escort istanbul bayan escort izmit escort izmit escort kocaeli escort sisli escort mecidiyekoy escort bursa escort bayan bursa görükle escort maltepe escort goztepe escort yakacik-yeni-escort bursa sınırsız escort sisli escort bayan taksim escort bursa escort porno izle sikiş izle atasehir escort antalya escort
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort
bedava bahis