Bootstrap / HTML form with jQuery post method to submit data without refresh

In this tutorial, I will show you using a Bootstrap or HTML form to submit data via jQuery $post method.

The post method is used to load data by using the HTTP request. Along with specifying the source you may send data along with it.

Similarly, you may send data taken from a user in an HTML or Bootstrap form by using post method and save it to database or use it to retrieve certain information without reloading the web page.

See the following demo online, enter some data and press Create account button. I will explain how it is done by using $.post method after that:

jQuery post form

Online demo and code

First of all, I created a Bootstrap form by using the Bootstrap framework CSS and its built-in classes. The CSS library is included in the head section of the code:

<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css” integrity=”sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==” crossorigin=”anonymous”>

Not to mention, you also need to include the jQuery library in the head section.

<script src=”http://code.jquery.com/jquery-1.10.1.min.js”></script>

Before going into the <script> section, let me describe briefly the presentation of the form.

The form is created by using the Bootstrap built-in classes, for example, it’s a horizontal form:

<form class=”form-horizontal” role=”form”>

Different controls are using different field sizes by specifying its class in div that contains it, e.g.

      <div class="form-group">

<label for="name1" class="col-sm-2 control-label">Name:</label>

<div class="col-sm-4">

<input type="text" class="form-control formcls" id="name1" placeholder="Enter Your Full Name">

</div>

</div>

 

Finally, a button is used outside of the form closing tag. The reason is, I have called the $.post method at the click event of this button.

I also used a custom CSS class to style the form fields, which is also placed in the head section.

The code of jQuery is placed at the <head> section:

<script>

$(document).ready(function(){

$("#postdata").click(function(){



postname=$("#postname").val();

postgender=$("#postgender").val();

postemail=$("#postemail").val();

postpass=$("#postpassword").val();

postaddress=$("#postaddress").val();





$.post("post-forms.php", {name:postname, gender:postgender, email:postemail, password:postpass, address:postaddress },function(postresult){

$("#postdiv").html(postresult);

});



});

});

</script>

 

First of all, the data is assigned to the variables in the jQuery code. After that, the $post method is used to specify the file where this data is sent. Multiple data fields are enclosed in curly brackets and separated by commas.

$.post("post-forms.php", {name:postname, gender:postgender, email:postemail, password:postpass, address:postaddress },function(postresult){

 

Before going to the next line in script, let me move to post-forms.php script file where action is happening as above line is executed:

First, the data sent by using $.post method is captured in PHP variables by using the $_POST array, that contains the data.

<?php

$post_name=$_POST["name"];

$post_gender=$_POST["gender"];

$post_email=$_POST["email"];

$post_password=$_POST["password"];

$post_address=$_POST["address"];

 

You can write the PHP script to save this information into a database like MySQL and return the success message. For the demo, I am simply printing the information by using PHP echo statement.

if( $post_name )



{



echo "Name entered is: ". $post_name ."<br>";

echo "Gender:" .$post_gender ."<br>";

echo "Email:" .$post_email ."<br>";

echo "Password:" .$post_password ."<br>";

echo "Address:" .$post_address ."<br>";





}

?>

 

The echo statement will not display any line by itself. This is basically returned data which is captured by the postresult in the success part of $.post function. Again see these lines in <script> section:

$.post("post-forms.php", {name:postname, gender:postgender, email:postemail, password:postpass, address:postaddress },function(postresult){

$("#postdiv").html(postresult);

});

 

Finally, the postresult parameter is used in the $.html jQuery function to display the returned data into the specified div.

Note: If you do not include Bootstrap library, the form will still work with $.post method. It will act as an ordinary HTML form. Just remove the redundant code in the form fields if you do not intend to use Bootstrap.

Related links: Bootstrap Forms | PHP echo statement