Create multi-step form submission in PHP using jQuery

multi-step form submission in PHP using jQuery
Create multi-step form submission in PHP using jQuery

Hi Friends, In this tutorial, we will learn how to create multi-step form submission in PHP using jQuery. It is very simple and in order to do this, please follow the below steps one by one.

  1. We have to create a table in our database.

I have a table name “stepformtbl” in my database.

DDL information of the table

CREATE TABLE stepformtbl (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
username varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
password varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
phone_no varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
address varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
created_at timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
updated_at timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

2. Create the input fields in HTML using active tabs and inactive tabs to identify the input fields according to the tabs. We will do this with the help of jQuery. In the HTML file, the id attribute of each input field is very important.

By using the id of input fields, we will do the validation part. Please follow the below HTML code to understand how the input fields are changing according to the navigation tabs.

HTML Code:-

<div class="container">
	  <h3 align="center"><u>Multi Step Form Submission in PHP using jquery </u></h3>
	  <br/><br/><br/>
		<ul class="nav nav-tabs">
	     <li class="nav-item">
	      <a class="nav-link active_tab1" style="border:1px solid #ccc;font-size: 14px;background-color: #fff;color: #333" id="list_user_details">User Details</a>
	     </li>
	     <li class="nav-item">
	      <a class="nav-link inactive_tab1" id="list_personal_details" style="background-color: #f5f5f5;color: #333;font-size: 14px;">Personal Details</a>
	     </li>
	    </ul> 
        <hr style="border: 1px solid black;">
		<form class="form-horizontal" action="" method="post">
		<div class="tab-content">
		     <div class="tab-pane active" id="user_details">
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Username">Username*:</label>
					    <div class="col-sm-8">
					      <input type="text" name="username" id="user" class="form-control form-control-sm">
					    </div>
				  </div>
				<br/>
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Password">Password*:</label>
					    <div class="col-sm-8">
					     <input type="password" id="pass" name="password" class="form-control form-control-sm">
					    </div>
				  </div>
				  <br>
				   <button type="button" class="btn-sm btn-primary" id="btn_us">Next</button>
		     </div>     
		     <div class="tab-pane" id="personal_details">
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Phone No">Phone No*:</label>
					    <div class="col-sm-8">
					      <input type="text" name="phone_no" class="form-control form-control-sm">
					    </div>
				  </div>
				<br/>
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Address">Address*:</label>
					    <div class="col-sm-8">
					     <textarea name="address" class="form-control" rows="5"></textarea>
					    </div>
				  </div>
				  <br>
				  <button type="button" name="previous_btn_personal_details" id="previous_btn_personal_details" class="btn btn-info btn-sm">Previous</button>
				   <button type="submit" class="btn-sm btn-primary" name="save">Save</button>
		     </div>    
		 </div>
				  
		</form>
</div>

If you look at the HTML code, I have created two navigation tabs with class names “active tab1” and “inactive tab1” with the id attribute of each inside the hyperlink tag.

Also read, Insert Multiple Rows into MySQL with PHP Using Foreach Arrays

jQuery multi-step form submission example

3. Validate the input fields using jQuery to proceed to the next step. If you do not enter the information in the first step or in the previous step then you can not proceed to the next step. This is done by only Jquery.

<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>

//step form validation
$('#btn_us').click(function(){
  //alert('ok');
  var user = $('#user').val();
  var password = $('#pass').val();
if(user==''){
  alert('Please enter the username');
}
if(password==''){
  alert('Enter the password');
}
else{
  $('#list_user_details').removeClass('active active_tab1');
   $('#list_user_details').removeAttr('href data-toggle');
   $('#user_details').removeClass('active');
   $('#list_user_details').addClass('inactive_tab1');
   $('#list_personal_details').removeClass('inactive_tab1');
   $('#list_personal_details').addClass('active_tab1 active');
   $('#list_personal_details').attr('href', '#personal_details');
   $('#list_personal_details').attr('data-toggle', 'tab');
   $('#personal_details').addClass('active in');
}
});
$('#previous_btn_personal_details').click(function(){
   $('#list_personal_details').removeClass('active active_tab1');
   $('#list_personal_details').removeAttr('href data-toggle');
   $('#personal_details').removeClass('active');
   $('#list_personal_details').addClass('inactive_tab1');
   $('#list_user_details').removeClass('inactive_tab1');
   $('#list_user_details').addClass('active_tab1 active');
   $('#list_user_details').attr('href', '#personal_details');
   $('#list_user_details').attr('data-toggle', 'tab');
   $('#user_details').addClass('active in');
});

if you look at the above code validation starts with the if else condition. If information is empty then it will ask you to enter the required information. If it is ok then it will move to the else condition.

Steps while clicking on the NEXT button

When we click on the NEXT button, the following classes of jquery are applied.

  • removeClass().
  • addClass().
  • removeAttr().
  • attr().
  • data-toggle property

data-toggle is basically used to jump from one div to another div.

If you want to know these classes in detail then follow this link Jquery classes with examples.

So, after clicking on the next button, we will remove the section of the active tab with the help of removeClass().

We will add the section of inactive tab with the help of addClass().

We will get all the attributes, input fields, and id of the personal details tab using the attr() method of Jquery.

4. If the validation is ok then it is time to submit the data to the database. We will do this with help of PHP.

<?php
include('connect.php');
if(isset($_POST['save']))
{
	$username = $_POST['username'];
	$password = $_POST['password'];
	$phone_no = $_POST['phone_no'];
	$address = $_POST['address'];
	//echo $username.$password.$phone_no.$address;
//insert into the table in the database
    $sql="INSERT INTO stepformtbl(username,password,phone_no,address)VALUES('$username','$password','$phone_no','$address')";
	$stmt=$con->prepare($sql);
	$stmt->execute();
	echo "<script type='text/javascript'>";
		echo "alert('Submitted successfully')";
		echo "</script>";
}
?>

Complete Code:-

<?php
include('connect.php');
if(isset($_POST['save']))
{
	$username = $_POST['username'];
	$password = $_POST['password'];
	$phone_no = $_POST['phone_no'];
	$address = $_POST['address'];
	//echo $username.$password.$phone_no.$address;
//insert into the table in the database
    $sql="INSERT INTO stepformtbl(username,password,phone_no,address)VALUES('$username','$password','$phone_no','$address')";
	$stmt=$con->prepare($sql);
	$stmt->execute();
	echo "<script type='text/javascript'>";
	echo "alert('Submitted successfully')";
	echo "</script>";
}
?>
<html>
<head>
<title>ajax example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<style>
.container{
	width:50%;
	height:30%;
	padding:20px;
}
</style>
</head>
<body>
   <div class="container">
	  <h3 align="center"><u>Multi Step Form Submission in PHP using jquery </u></h3>
	  <br/><br/><br/>
		<ul class="nav nav-tabs">
	     <li class="nav-item">
	      <a class="nav-link active_tab1" style="border:1px solid #ccc;font-size: 14px;background-color: #fff;color: #333" id="list_user_details">User Details</a>
	     </li>
	     <li class="nav-item">
	      <a class="nav-link inactive_tab1" id="list_personal_details" style="background-color: #f5f5f5;color: #333;font-size: 14px;">Personal Details</a>
	     </li>
	    </ul> 
        <hr style="border: 1px solid black;">
		<form class="form-horizontal" action="" method="post">
		<div class="tab-content">
		     <div class="tab-pane active" id="user_details">
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Username">Username*:</label>
					    <div class="col-sm-8">
					      <input type="text" name="username" id="user" class="form-control form-control-sm">
					    </div>
				  </div>
				<br/>
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Password">Password*:</label>
					    <div class="col-sm-8">
					     <input type="password" id="pass" name="password" class="form-control form-control-sm">
					    </div>
				  </div>
				  <br>
				   <button type="button" class="btn-sm btn-primary" id="btn_us">Next</button>
		     </div>     
		     <div class="tab-pane" id="personal_details">
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Phone No">Phone No*:</label>
					    <div class="col-sm-8">
					      <input type="text" name="phone_no" class="form-control form-control-sm">
					    </div>
				  </div>
				<br/>
				  <div class="form-group">
				    <label class="control-label col-sm-2" for="Address">Address*:</label>
					    <div class="col-sm-8">
					     <textarea name="address" class="form-control" rows="5"></textarea>
					    </div>
				  </div>
				  <br>
				  <button type="button" name="previous_btn_personal_details" id="previous_btn_personal_details" class="btn btn-info btn-sm">Previous</button>
				   <button type="submit" class="btn-sm btn-primary" name="save">Save</button>
		     </div>    
		 </div>
				  
		</form>
</div>
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>

//step form validation
$('#btn_us').click(function(){
  //alert('ok');
  var user = $('#user').val();
  var password = $('#pass').val();
if(user==''){
  alert('Please enter the username');
}
if(password==''){
  alert('Enter the password');
}
else{
  $('#list_user_details').removeClass('active active_tab1');
   $('#list_user_details').removeAttr('href data-toggle');
   $('#user_details').removeClass('active');
   $('#list_user_details').addClass('inactive_tab1');
   $('#list_personal_details').removeClass('inactive_tab1');
   $('#list_personal_details').addClass('active_tab1 active');
   $('#list_personal_details').attr('href', '#personal_details');
   $('#list_personal_details').attr('data-toggle', 'tab');
   $('#personal_details').addClass('active in');
}
});
$('#previous_btn_personal_details').click(function(){
   $('#list_personal_details').removeClass('active active_tab1');
   $('#list_personal_details').removeAttr('href data-toggle');
   $('#personal_details').removeClass('active');
   $('#list_personal_details').addClass('inactive_tab1');
   $('#list_user_details').removeClass('inactive_tab1');
   $('#list_user_details').addClass('active_tab1 active');
   $('#list_user_details').attr('href', '#personal_details');
   $('#list_user_details').attr('data-toggle', 'tab');
   $('#user_details').addClass('active in');
});
</script>
</body>
</html>
NOTE*

Download the bootstrap css and js files from google and include the path of the files in the href attribute of link tag and src attribute of the script tag respectively.

Also Read, CRUD Operation In Laravel 5.8 Step By Step For Beginners

CONCLUSION:- I hope this article will help you to create multi-step form submission in PHP using jQuery. If you have any doubt then please leave your comment below.

3 thoughts on “Create multi-step form submission in PHP using jQuery”

  1. Excellent post. I used to be checking continuously this weblog and I am impressed!

    Very useful information specially the closing part
    🙂 I care for such info a lot. I was seeking this certain info for a very long time.
    Thanks and good luck.

    Reply

Leave a Comment

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311