How to Store Form Data in Session in PHP


How to Store Form Data in Session in PHP

Edit Post

Site Icon

Update

How to Store Form Data in Session in PHP

How to Store Form Data in Session in PHP

In this tutorial, we will learn how to store form data in a session in PHP. We will take an example as shown in the above image with the help of an HTML form.

Using session, we can keep the form values even after submitting the data. If you do not know about the PHP session and how it works then go through the below tutorial link.

Also Read, Login and Logout Using Session in PHP and MySQLi

Required steps to store form data in session in PHP

  • First of all, start the PHP session with the help of the session_start() function.
  • We will get the form values from the request after submitting with the help of PHP $_POST[] global variable in the form of an array.
  • Now, we will store all the POST variables in a session array with the help of the $_SESSION[] global variable in the form of an array in PHP.
  • The form values will remain in session until we click the reset button.
  • After clicking the reset button, all the form values will be removed from the form fields with the help of the session_unset() function.

Complete Code:-

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.

Conclusion:- I hope this tutorial will help you to understand the overview. If you have any doubts then please leave a comment below. Know more about PHP sessions.



Sassy Social Share

Toggle panel: Sassy Social Share

Yoast SEO

Toggle panel: Yoast SEO

Focus keyphraseHelp on choosing the perfect focus keyphrase(Opens in a new browser tab)

Get related keyphrases(Opens in a new browser tab)

Search appearance

Determine how your post should look in the search results.Preview as:Mobile resultDesktop result

Url preview:

PBPhpsolutionspbphpsolutions.com› how-to-store-form-data-in-session-in-php

SEO title preview:

How to Store Form Data in Session in PHP – PBPhpsolutions

Meta description preview:

May 9, 2021 - Learn how to store the form data in session even after submitting in PHP and also destroy the session using the session_unset() function.

SEO titleUse AIInsert variable

Title Separator Site title

Slug

Meta descriptionUse AIInsert variable

Learn how to store the form data in session even after submitting in PHP and also destroy the session using the session_unset() function.

SEO analysisGoodStore Form Data in Session

Premium SEO analysisPremiumAdd related keyphrasePremiumInternal linking suggestionsPremiumTrack SEO performance

Cornerstone content

Advanced

Insights

In this tutorial, we will learn how to store form data in a session in PHP. We will take an example as shown in the above image with the help of an HTML form.

Using session, we can keep the form values even after submitting the data. If you do not know about the PHP session and how it works then go through the below tutorial link.

Also Read, Login and Logout Using Session in PHP and MySQLi

Required steps to store form data in session in PHP

  • First of all, start the PHP session with the help of the session_start() function.
  • We will get the form values from the request after submitting with the help of PHP $_POST[] global variable in the form of an array.
  • Now, we will store all the POST variables in a session array with the help of the $_SESSION[] global variable in the form of an array in PHP.
  • The form values will remain in session until we click the reset button.
  • After clicking the reset button, all the form values will be removed from the form fields with the help of the session_unset() function.

Complete Code:-

<?php
session_start();
if(isset($_POST['save'])){
    //store the session data
	$_SESSION['username'] = $_POST['username'];
	$_SESSION['email'] = $_POST['email'];
	$_SESSION['phone_no'] = $_POST['phone_no'];
    //print_r($_SESSION);
}
//reset the form data
if(isset($_POST['reset'])){
	session_unset();
	//session_destroy();
}
?>
<html>
<head>
<title>How to Store values in session in php</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">
   	 <form class="form-horizontal" action="" method="post">
   	 	  <div class="form-group">
		    <label class="control-label col-sm-2" for="email">Name:</label>
		    <div class="col-sm-10">
		      <input type="text" value="<?php if(isset($_SESSION['username'])) echo $_SESSION['username'];?>" class="form-control" name="username" placeholder="Enter Name">
		    </div>
		  </div>
		  <div class="form-group">
		    <label class="control-label col-sm-2" for="email">Email:</label>
		    <div class="col-sm-10">
		      <input type="email" value="<?php if(isset($_SESSION['email'])) echo $_SESSION['email'];?>" class="form-control" name="email" placeholder="Enter email">
		    </div>
		  </div>
		  <div class="form-group">
		    <label class="control-label col-sm-2" for="email">Phone No:</label>
		    <div class="col-sm-10">
		      <input type="number" value="<?php if(isset($_SESSION['phone_no'])) echo $_SESSION['phone_no'];?>" class="form-control" name="phone_no" placeholder="Enter Phone No">
		    </div>
		  </div>
		  <div class="form-group">
		    <div class="col-sm-offset-2 col-sm-10">
		      <button type="submit" name="save" class="btn btn-success">Submit</button>
		      <button type="submit" name="reset" class="btn btn-danger">Reset</button>
		    </div>
		  </div>
	</form>
   </div>
<br/><br/>
<!--End edit Model-->
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>

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.

Conclusion:- I hope this tutorial will help you to understand the overview. If you have any doubts then please leave a comment below. Know more about PHP sessions.


2 thoughts on “How to Store Form Data in Session in PHP”

  1. Does your site have a contact page? I’m having a tough time locating it but, I’d like to shoot you an email.

    I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it grow over time.

    Reply
  2. Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Regardless, just wanted to say great blog!

    Reply

Leave a Comment

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

sabung ayam online

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 DEMO SLOT

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

parlay

HOKI311

THOR311

Thor311 Aplikasi

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311