This post help to understand multi-step form submission with progress bar using PHP, jQuery and bootstrp3. Multi steps form is a very useful functionality when the user input data is too much and needs to divide into parts. We will divide user inputs into steps and associate these steps with each other using navigation like tab or pills.
We will gather all these steps data and submit all inputs on the final step of the HTML Form.I am using non ajax method to submit data using php, You can use ajax submit using jQuery.
There are following steps will follow on this article,
action.php
file.If you are more comfortable in watching a video that explains about Multi Step Form Using PHP, Bootstrap and jQuery, then you should watch this video tutorial.
Lets create multi step form using bootstrap.
Step 1: We will create index.php
file, where we need to test this sample example. I need to include jQuery and bootstrap library files into the head
section of index.php
file.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
Here i am using cdn
path for jquery and bootstrap. You can also use these library locally and replace with your path.
Step 2: Creating HTML UI for all steps which you need in multi step form to follow user and added into index.php
file.
<div class="progress"> <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100"></div> </div> <form id="regiration_form" novalidate action="action.php" method="post"> <fieldset> <h2>Step 1: Create your account</h2> <div class="form-group"> <label for="email">Email address</label> <input type="email" class="form-control" id="email" name="email" placeholder="Email"> </div> <div class="form-group"> <label for="exampleInputPassword1">Password</label> <input type="password" class="form-control" id="password" placeholder="Password"> </div> <input type="button" name="password" class="next btn btn-info" value="Next" /> </fieldset> <fieldset> <h2> Step 2: Add Personnel Details</h2> <div class="form-group"> <label for="fName">First Name</label> <input type="text" class="form-control" name="fName" id="fName" placeholder="First Name"> </div> <div class="form-group"> <label for="lName">Last Name</label> <input type="text" class="form-control" name="lName" id="lName" placeholder="Last Name"> </div> <input type="button" name="previous" class="previous btn btn-default" value="Previous" /> <input type="button" name="next" class="next btn btn-info" value="Next" /> </fieldset> <fieldset> <h2>Step 3: Contact Information</h2> <div class="form-group"> <label for="mob">Mobile Number</label> <input type="text" class="form-control" id="mob" placeholder="Mobile Number"> </div> <div class="form-group"> <label for="address">Address</label> <textarea class="form-control" name="address" placeholder="Communication Address"></textarea> </div> <input type="button" name="previous" class="previous btn btn-default" value="Previous" /> <input type="submit" name="submit" class="submit btn btn-success" value="Submit" /> </fieldset> </form>
I have created 3 steps using HTML field-set control for each step, So whenever user click next and previous button. We will slide fieldset based on current step.
Step 3: Add css class into section of
index.php
file to hide other steps except first step.
<style type="text/css"> #regiration_form fieldset:not(:first-of-type) { display: none; } </style>
Step 4: We will use jQuery to hide and show HTML fieldset for navigation between steps,You need to add below code into footer of index.php
file.
$(document).ready(function(){ var current = 1,current_step,next_step,steps; steps = $("fieldset").length; $(".next").click(function(){ current_step = $(this).parent(); next_step = $(this).parent().next(); next_step.show(); current_step.hide(); setProgressBar(++current); }); $(".previous").click(function(){ current_step = $(this).parent(); next_step = $(this).parent().prev(); next_step.show(); current_step.hide(); setProgressBar(--current); }); setProgressBar(current); // Change progress bar action function setProgressBar(curStep){ var percent = parseFloat(100 / steps) * curStep; percent = percent.toFixed(); $(".progress-bar") .css("width",percent+"%") .html(percent+"%"); } });
Step 5: Created action.php
file and added below code into this file.
<div class="row well alert alert-success"><?php echo "<pre>";print_R($_POST);?></div>
So after completion of all steps user will click submit button. We will post all steps data and send to action.php
file using the form action attribute.
You can download source code and Demo from below link.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
how can you make it somehow it sends the info to admin email?
You can use javascript send to mail function or you can use backend email programming function like in php there is email() function
you can create your own validation rule or can be use jquery plugin for that.
Nice writeup, although I notice a tiny bit of issue with your approach. If I add a container 'div' or 'p' and wrap the next/prev buttons inside the container, it will stop working. This is because the javascript is making assumption that 'fieldset' is the parent element for the next/prev buttons. If this condition isnt satisfied, it will not work. My suggestion is that, instead of looking for the parent element for next/prev buttons, its better to look for the closest fieldset element. So change $(this).parent() to $(this).closest("fieldset"), just my two cents.
you can add thank you page once all steps completed and send to this page using php redirect method, for data you can store into session/localstorage or cookie between steps.
thanks sir, you safe my life.
you, using HTML5 , You can get basic use of localstorage from https://www.js-tutorials.com/javascript-tutorial/html5-localstorage-sessionstorage-using-angularjs/.
Hi thanks very much for the scripts!
Is there a way to link to the second step of the flow if the first step is a something optional?
yes, You can update jquery navigation code