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'];
Related
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'];
Sorry if this is a duplicate, but I really cant find anything that could solve my problem. I can pass numbers and strings like $_SESSION['blabla']="123'; but I can't pass this $_POST value from the textfield and submit button.
Page 1 (sessions.php)
<?php session_start(); ?>
!doctype stuff here
<body>
<form id="form1" name="form1" method="post" action="sessions2.php">
<label>
<input type="text" name="damn" id="damn" />
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
<?php
$omg = $_POST['damn'];
$_SESSION['damn'] = $omg;
echo $_SESSION['damn'] ;
?>
Page 2 (sessions2.php)
<?php
session_start();
$fires = $_SESSION['damn'];
echo "wth";
echo $_SESSION['damn'];
?>
PS. Sorry for the names.. I'm truly stumped.
You need to put the code that reads from $_POST in the file that you submit the form to.
Currently your process is:
Get request for sessions.php
Send form to browser
Assign $_POST['damn'] (which is undefined) to the session.
User submits form
Get request for sessions2.php
Ignore $_POST (which is now populated)
Read from the session (where the variable is still undefined).
damn is populated in the form submission request (step 4/5) not the request where you are trying to read it (step 1).
In sessions2.php
// you POST "damn" variable via form, using post method, so:
$fires = $_POST['damn'];
// and:
$_SESSION['damn'] = $fier;
// or
$_SESSION['damn'] = $_POST['damn'];
PHP code in file sessions.php doesn't work, because in form action you have session2.php.
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']
I want to make a form which takes information then uses that information on another page when it is submitted. However once it redirects, it loses all the information from the other page for example:
Page 1:
<?php
if(isset($_POST['submit']))
{$info=$_POST['info'];}
?>
<html>
<form action='page2.html' method='POST'>
<input name='info'>
<intput type='submit' name='submit'>
</form>
</html>
Page2:
<?php
echo $info;
?>
it doesn't know what the variable 'info' is on page 2.
Add:
if(isset($_POST['submit']))
{ echo $_POST['info'];}
and remove:
echo $info;
Now reasons:
When you submit a form it's redirected to a page written in action attribute of form tag and sends form data to it. So after submitting form you are on the page2 where you have access to posted data.
If you are posting to page2.html then your form data will be in the global $_POST variable. Try
print_r($_POST);
in your page2.html php
in page 2:
echo $_POST['info'];
I don't know why you have the variables $_POST in the first page. Also you should name the page2 page2.php unless you have configured it otherwise in your web server.
Page 1:
<html>
<form action='page2.html' method='POST'>
<input name='info'>
<intput type='submit' name='submit'>
</form>
</html>
Page 2:
echo $_POST['info'];
Or, to see everything that's passed between pages:
print_r($_POST);
I can pass values form one page to another but I need to pass value like this,
Page 1:
Page4.php
Page3.php
I need to pass the value in a text field in the Page1.php to a text field in Page2.php, since the form is not directly redirectly to page2, I am unable to pass the value, I tried session, form post method and few other methods but I am yet to succeed.
I would be very happy if you can help me with the code or some suggestions.
Thanks!
Edit..........
I found the answer, thanks for the help, it was actually a careless mistake on my part, I used $_post instead of $_session.
Its working now.
Thanks for the help.
Use something like this:
page1.php
<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>
Any other PHP page:
<?php
session_start();
echo $_SESSION['myValue'];
?>
A few notes to keep in mind though: You need to call session_start() BEFORE any output, HTML, echos - even whitespace.
You can keep changing the value in the session - but it will only be able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.
The setting of the variable itself can be done in one of a number of ways:
$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];
And if you want to check if the variable is set before getting a potential error, use something like this:
if(!empty($_SESSION['myValue'])
{
echo $_SESSION['myValue'];
}
else
{
echo "Session not set yet.";
}
Solution using just POST - no $_SESSION
page1.php
<form action="page2.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
page2.php
<?php
// this page outputs the contents of the textarea if posted
$textarea1 = ""; // set var to avoid errors
if(isset($_POST['textarea1'])){
$textarea1 = $_POST['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
Solution using $_SESSION and POST
page1.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
$textarea1 = "";
if(isset($_POST['textarea1'])){
$_SESSION['textarea1'] = $_POST['textarea1'];
}
?>
<form action="page1.php" method="post">
<textarea name="textarea1" id="textarea1"></textarea><br />
<input type="submit" value="submit" />
</form>
<br /><br />
Go to page2
page2.php
<?php
session_start(); // needs to be before anything else on page to use $_SESSION
// this page outputs the textarea1 from the session IF it exists
$textarea1 = ""; // set var to avoid errors
if(isset($_SESSION['textarea1'])){
$textarea1 = $_SESSION['textarea1']
}
?>
<textarea><?php echo $textarea1;?></textarea>
WARNING!!! - This contains no validation!!!