Using correctly $_SESSION to give values to another PHP page.

Hello all. I’m working on a form but I can’t get the following task:

If a user don’t compile all the form, I want to pickup from another php script the POST values and insert them in a SESSION, so reload the form page but with the inputs, which the used compiled previously, with their values has SESSION vars.

This is my code but it doesn’t work…
formpage.php
[php]<?php session_start(); ?>
… some html code …

<?php $sessiondata['name'] = ""; $sessiondata['lastname'] = ""; if( isset($_SESSION['name']) || isset($_SESSION['lastname']){ $sessiondata['name'] = $_SESSION['name']; $sessiondata['lastname'] = $_SESSION['lastname']; }?> > > ...submit tag... ...some html code... [/php]

post.php
[php]
session_start();

if($_POST[‘name’] || $_POST[‘lastname’]) { … some todo code … } else { $_SESSION[‘name’] = $_POST[‘name’]; $_SESSION[‘lastname’] = $_POST[‘lastname’]; header(“Location: formpage.php”); exit; }
[/php]

How about…

<?php // session_start(); $fname=$_GET['first']; $lname=$_GET['last']; ?> ... some html code ... ...submit tag... ...some html code...

and your post.php…

$fname=$_POST['name']; $lname=$_POST['lastname'];

If(($fname != “”) && ($lname != “”)) {
// We have data in both fields - process data…
}else{
// one or both fields are empty! Return back to the form page with whatever info we have
header(“Location: formpage.php?first=$fname&last=$lname”)
}

This can be done in javascript also.

You weren’t using proper syntax for you input fields.

Ok, but if there are lots of values to get I don’t want to make a long url (expecially if there is a password to get). How can I do that?

If the user is using a form on page 1, which then sends them to page2 which is another form, on page 2 just have some hidden inputs which have the values from page1 using $_POST

I don’t think you know how sessions work.

To set a session value, you use $_SESSION[‘name’]=“John”;
To get a session value, you use $name = $_SESSION[‘name’];

You are checking if the session isset like this:
[php]if( isset($_SESSION[‘name’]) || isset($_SESSION…[/php]

But then you use this to get the value:
[php]<?php echo("value='"+$sessiondata['name']+"'");?>[/php]

Where did you ever set the value of $sessiondata[‘name’] ??

You should be using this:
First set the value: $_SESSION[‘name’]=“John”;

Then use it like this:
[php]<?php echo("value='"+$_SESSION['name']+"'");?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service