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

THOR311

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

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

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

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

KUPU178

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