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:-

<?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.


Leave a Reply

Your email address will not be published. Required fields are marked *

Theme of 96 THEME.