Storing Form Data as a Session Variable

store data from form to session as variable

Your Code does not contain any error and it will work fine.

You can use the isset() while calling it over to the code and hence it avoid the errors that display up in your browser.

Replace:

<strong><?php echo $_SESSION['cars'];?></strong>

With:

<?php
session_start();
ob_start();
error_reporting(0);// This will depreciate all errors
if(isset($_SESSION['cars']))
{
echo '<strong>'.$_SESSION['cars'].'</strong>';
}
else
{
// Handle failure over here
}
?>

But as you have mentioned that you have got confused at the session() am explaining about the session and the form attributes that you have used below.

Explanation

Will provide you with the clear explanation of what the process happens.

One:

if (isset($_POST['Submit'])) {}

This will handle up the form submit operation. Once after the form is submitted this action will take place.

Provided the name of the submit button should be as name="Submit" and then it will trigger up this action on submit of the form.

Two:

<select name="cars"></select>

When you need to fetch out the data after the form is submitted you need to provide with the name for the select tag then alone you can get the data from the option that is being selected.

Three:

<option value="camry" name="camry">Camry</option>

Option does not contain the name in the HTML and you have to delete the name which you have provide to the option tag.

Four:

After all this is over we are now going for the session_start() in the PHP.

session_start — Start new or resume existing session.

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers. These will either be a built-in save handler provided by default or by PHP extensions (such as SQLite or Memcached); or can be custom handler as defined by session_set_save_handler(). The read callback will retrieve any existing session data (stored in a special serialized format) and will be unserialized and used to automatically populate the $_SESSION superglobal when the read callback returns the saved session data back to PHP session handling.

To use a named session, call session_name() before calling session_start().

When session.use_trans_sid is enabled, the session_start() function will register an internal output handler for URL rewriting.

Reference: http://php.net/manual/en/function.session-start.php

How to Use Sessions in Your PHP Scripts

  1. Starting a Session
  2. Storing and Accessing Variables
  3. Ending a Session

Storing form data in session with php

The first error I notice is this piece of code:

<form action="" method"post">

Where method does not contain the symbol "=" which causes the loss of the parameter post.
Furthermore, the "session_start ()" function must be placed before any other code. The code derives from this is as follows:

<?php
// starting the session
session_start();

if (isset($_POST['Submit'])) {
$_SESSION['var'] = $_POST['var'];
$name = $_SESSION['var'];
} else {
$name = null;
}
?>

<strong>Test Form</strong>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="var" value="<?= ($name != null) ? $name : ''; ?>">
<input type="submit" name="Submit" value="Submit!" />
</form>

Storing form data as a temporary session

Just clear the session data when it is not needed. You can do an unset against your temporary variables.

But for the same reason, I think it would be better to store the form one level deeper:

$_SESSION['formdata'] = $_POST; // Save the form

Then you can retrieve username as $_SESSION['formdata']['username'] or unset $_SESSION['formdata'].

Store On Change and Later Retrieve Session Form Data

Lot f ways to do this. Here is one example that uses localStorage.

Working Example: https://jsfiddle.net/Twisty/ofrcza1g/

HTML

<div>
<form id="myForm">
<label for="myName">Name</label> <input type="text" id="myName" /><br />
<label for="myTitle">Title</label> <input type="text" id="myTitle" /><br />
<label for="myGender">Gender</label> <select id="myGender">
<option></option>
<option>Male</option>
<option>Female</option>
<option>Non-Binary</option>
</select>
</form>
</div>
<button id="myStatus">Check Status</button>

JavaScript

$(function() {
var dataSaved = false;

function saveData(i, s) {
// Input: Index, String
// Output: Boolean
localStorage.setItem(i, s);
return true;
}

function loadData(i) {
// Input: Index
// Output: Parse Variable Data
var d = localStorage.getItem(i);
return JSON.parse(d);
}

function gatherData(f) {
var d = {};
f.find("input, select, textarea").each(function(i, el) {
var k = $(el).attr("id");
var v = $(el).val();
d[k] = v;
});
return d;
}

$('#myForm').on('change blur', 'input, select, textarea', function(e) {
var fd = JSON.stringify(gatherData($("#myForm")));
console.log("Saving", fd);
dataSaved = saveData("fd", fd);
});
$("#myForm").ready(function() {
var fd = loadData("fd");
console.log(fd);
$.each(fd, function(k, v) {
$("#" + k).val(v);
});
});
$("#myStatus").click(function() {
console.log(loadData("fd"));
});
});

You can use .serialize() too. I just found that it relies on name attribute.

Hope this helps.



Related Topics



Leave a reply



Submit