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