Storing Form Data as a Session Variable on Multiple Pages - php

I just wondering if its possible to store form data as Session on Multiple Pages?
I have 5 Pages on my Site, every page has an form with an input field on it.
I want save all Sessions and input values on all 5 Pages, and echo it on the last page.
I had saved the session_start(); in one file and include it on every page:
session_start.php
<?php
session_start();
?>
On the top of page before <!DOCTYPE html> i add it like this on every page:
Page 1:
<?php
include("config.php");
require_once("session_start.php");
if (isset($_POST['submit'])) {
$_SESSION['email'] = $_POST['email'];
}
?>
then in the Body:
<form action="" method"post">
<input type="text" name="email"/>
<input type="submit" name="submit" value="Enter" />
</form>
Page 2:
<?php
include("config.php");
require_once("session_start.php");
if (isset($_POST['submit'])) {
$_SESSION['passwort'] = $_POST['passwort'];
}
?>
then in the Body again:
<form action="" method"post">
<input type="text" name="passwort"/>
<input type="submit" name="submit" value="Enter" />
</form>
I can echo the email Session on the Page 2 without Problems with:
<?php
echo $_POST["email"];
?>
But get an error on the page Undefined index: passwort if i do it on the same way as in Page 1.
And continue on the other 3 Pages, whats wrong with my Way?
With my code here i can save the Session only from the Page before and echo it on the next page.
Thanks for looking!

So long as you are successfully calling session_start(); at the start of each new page load, you will be carrying the previously store session data forward.
Yes, you will need to transfer the form-submission data from $_POST to the $_SESSION array each time.
After this snippet on page 1:
if (isset($_POST['email'])) {
$_SESSION['email'] = $_POST['email'];
}
Your session will contain:
$_SESSION = ["email" => "example#email.com"];
Then after this snippet on page 2:
if (isset($_POST['passwort'])) {
$_SESSION['passwort'] = $_POST['passwort'];
}
Your session will contain:
$_SESSION = [
"email" => "example#email.com",
"passwort" => "p#$$w()rt"
];
And so on, for the pages to follow.
When you want to check what is in your array, you can simply call var_export($_SESSION).
When you want to access particular elements, use their key.
E.g. echo $_SESSION['passwort'];

Related

Undefined index for Session variable

I am setting a session variable when a submit button is pressed like so:
<?php
$submit = #$_POST["submit"];
if($submit){
$_SESSION['id'] = $id;
}
?>
<form action="add.php" method="POST">
<input type="submit" name="submit" value="Add">
</form>
However, in the page add.php when I do:
print_r($_SESSION['id'];
I get the following error:
Undefined index: id
I'm new to sessions so still trying to come to grips with them, but I thought the part where I do $_SESSION['id'] = $id; is where I define the index id to be the value of the $id variable? Could someone explain where I am going wrong?
UPDATE:
In case you're wondering if I am using session_start(): At the top of the page with the form, I include my header page which at the top contains require_once './init.php';. At the top of my add.php I just have the line require_once './init.php'; (I do not include the header file in add.php as this page will redirect as soon as it has executed its code.
In the init.php file is the following:
<?php
session_start();
require_once 'configurate.php'; //database info
?>
you should call session_start() before setting any values in $_SESSION
change your code like this
<?php
session_start();
$submit = #$_POST["submit"];
if($submit){
$_SESSION['id'] = $id;
}
?>
<form action="add.php" method="POST">
<input type="submit" name="submit" value="Add">
</form>
I hope you have initialized your session via session_start before setting and retrieving the value from $_SESSION
if the value submitted via $_POST["submit"] compares to false (e.g. "" or 0) your condition if($submit) won't succeed, better use if (isset($_POST['submit'] instead

Session variable gets lost between pages

I have numerous pages that I need to access a variable on. This variable is assigned a value when a user enters an ID into a form on accounts.php:
account.php
<form action="afterlog.php" class="form" method="post">
<input type="text" name="amid" id = "amid" class="input" />
<input class="btn" type="submit" value="Go" />
</form>
which posts the value 'amid' to afterlog.php
afterlog.php
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
header('Location: customer_view.php');
}
?>
which checks the POST, assigns the session variable, and redirects rhe user to customer_view.php
customer_view.php
I need to use '$account_manager_id' in this page and all pages after. Here is how I am assigning it the value of the _SESSION variable:
<?php
session_start();
$_SESSION['account_manager_id']=$account_manager_id;
?>
Bu the value isn't being held on any of the pages, including customer_view.php. I know its passing to afterload.php because it prints out on that page, but its gone after that page.
What I am doing wrong?
Thanks for your help!
You are trying to assign a value to $_SESSION['account_manager_id'] before $account_manager_id has any value in it. You just need to switch the order:
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
or simply:
$_SESSION['account_manager_id'] = $_POST['amid'];
in afterlog.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$account_manager_id = $_POST['amid'];
$_SESSION['account_manager_id']=$account_manager_id;
header('Location: customer_view.php');
}
?>
or
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$_POST['amid'];
header('Location: customer_view.php');
}
?>

Session Variable not sending to next page

I am trying to set a session variable for the location selected in the dropdown menu when the user hits submit. The goal is to pass this variable to another php page so that I can display the drop down option they chose along with two values associated with it from a MYSQL table.
Below is a section of my code from my first php file. It shows the dropdown that is created from a list of values from the MYSQL database along with where I am trying to set the session variable for the selected option.
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
Below is my code from the second page where I call the session variables for display. As you can see I have input another variable for testing purposes. It is supposed to display "cat" and it does.
session_start();
echo $_SESSION['animal'];
echo $_SESSION['locations'];
It may also be important to note that locationsprocessing.php runs a redirect using header. But that is really all that file does.
Am I missing something? I have gone through countless different tuts and such and been staring at this code forever. I can not see why it wo
you need start session in every pages where you need to use session:
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
<?php
if (isset($_POST['submit'])) {
session_start();
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
}
This may be going wrong: When you submit your form in the first piece of code from the questions, you are sending the data entered in the form to /locationsprocessing.php. You say that all that file does is redirecting to another page. That means the data entered in the form is effectively lost.
I think what you want to do is to remove the form processing code from the first piece of code, so with only this remaining:
session_start();
<form action="/locationsprocessing.php" method="post">
<select id="locations" name="locations"><? echo $option; ?></select>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
Then place that removed piece of code in /locationsprocessing.php, like this:
session_start();
if (isset($_POST['submit'])) {
$_SESSION['locations'] = $_POST['locations'];
$_SESSION['animal'] = 'cat';
header('Location: ' . $destination);
}
$destination would be the path for the page with the second piece of code in the question.

Carry POST data to another page

I have a php form that at the moment posts the data to a Paypal cart, but I want to post the data to a new php page almost like a confirmation page which shows the options and selections you selected to and from that point for you to be able to send on to Paypal?
Is this possible?
Someone new to session or post may find this example helpful. complete example of what you want. how to Store and view post array into a session named POSTDATA.post or anyother variables are accessible to a same session, from any page if its stored in a session variable.
form.php
<?php
//PHP content of form.php file
session_start();
if ($_POST)
{
$_SESSION['POSTDATA']=$_POST;
}
?>
<html>
<head></head>
<body>
<form action="form.php" method="post">
<input type="text" name="hello"/>
<input type="submit" name="submit" value="submit"/>
</form>
<!--link to some another page for testing-->
</body>
</html>
anotherpage.php
<?php
session_start();
echo "This is some another page somewhere inside.ANOTHER PAGE SAYS";
echo '<pre>';
print_r($_SESSION['POSTDATA']);
echo '<pre>';
echo "INPUT BOX VALUE ".$_SESSION['POSTDATA']['hello'].'<br/>';
echo "SUBMIT BUTTON VALUE ".$_SESSION['POSTDATA']['submit'];
?>
One possibility is to store the post data in $_session['post'] = $_POST to persist the storage and get the posted data on the new page with $_session['post']['field']

Session variable doesnt change

I want to pass one session variable to other page. On my first page I have:
<?php
session_start();
?>
then:
<form class="form1" method="post" action="contact2.php" id="form1">
<ul>
<li>
<label for="name">*Name:</label>
<input type="text" name="name" placeholder="Black Nova"class="required" role="input" aria-required="true"/>
</li>
<li>
<input id="submit" class="submit .transparentButton" value="Next" type="submit" name="submit"/>
</li>
</ul>
<br/>
</form>
<?php
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];
?>
In my contact.php I have session start, but I cannot get sesssion variable.
If, in my first page, I dont give any action, I get the right value in $_SESSION['name'] but if I give an action, the session variable wont change. Why?
If you do not give action, then the form is being submitted into the same page and so $_POST has value because of which you can get it assigned to $_SESSION. When you give action as contact2.php the form is submitted into a different page and so $_POST will not be available in the page that has the form and so the session will not get any value from it.
If you have set action to contact2.php, you can do a session_start() in that page and move the code
<?php
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];
?>
into that page, and you should be able to echo the session in contact2.php
Its because if you don't supply any action, the form will submit to itself (same page) and because your setting the session variable in the first page it fills the session variable fine. When you add the action to the 2nd page it never fills the session variable because the first page isn't receiving the $_POST, the 2nd page is.
if your submitting form data to a different page, you need to set the session data on the receiving page not the sending page.
When you click submit in your form, form will send you in contact2.php (action="contact2.php").
Create contact2.php and write code below:
session_start();
session_regenerate_id();
$_SESSION['name'] = $_POST['name'];
echo $_SESSION['name'];

Categories