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

THOR311

ceri188

ceri188

ceri188

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

RATU311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

RATU311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

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

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311