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

See 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.

 

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:

 

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.

 

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.

 

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.

 

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:

 

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