Session variables in multiple forms

A

Anonymous

Guest
Hey, I am trying to have session vars carried on from one form to another. I have about 3 different forms on 3 different pages and all that information from the user needs to be all together, so I thought the best way would be to have session variables and at the end (on the 4th page) display all this info from those vars.

The problem is I can't find any good tutorial or anything on how to setup session variables that would travel from one form to another and then another.
Anybody have any advice?

Thanks.
 
Just store all the forms data in session variables.
For this case use session.use_cookies = On (.ini)
Also use session_set_cookie_params(0); to set your session litime (0 = til the browser is closed). This will prevent sessions' ending while the user is filling the form.

Example:
page 1:
Code:
<?php

// Form 1 here

?>
Page 2:
Code:
<?php

if(isset($_POST['FormSubmited_1'])){
   // Whatever fields you have in your form 1
   $_SESSION['form1_name'] = $_POST['form1_name'];
   $_SESSION['form1_email'] = $_POST['form1_email'];
}

?>

// Form 2 here
Page 3:
Code:
<?php

if(isset($_POST['FormSubmited_2'])){
   // Whatever fields you have in your form 2
   $_SESSION['form2_pass'] = $_POST['form2_pass'];
   $_SESSION['form2_re-pass'] = $_POST['form1_re-pass'];
}

?>

// Your form 3 here
And so on...
In every page you'll have previous pages' data available in your session variables by accessing it!
Example:
Code:
<?php  print 'Your name is: ' . $_SESSION['form1_name']; ?>
 
Alright, I am really new to PHP, right now I have this, and for some reason nothing works:
:(

A.php
====

Code:
<html>
<body>
<?php
echo "<form name=form1 method=post action=b.php>
<input type=text name=name>Name<br>
<input type=submit value=go></form>";
?>
</body>
</html>

B.php
====

Code:
<?php
session_start();
?>

<?php
if(isset($_POST['form1'])){
   // Whatever fields you have in your form 1
   $_SESSION['name1'] = $_POST['name'];
}
?>

<html>
<body>
<?php  print 'Your name is: ' . $_SESSION['name1']; ?>
<?php
echo "<form name=form2 method=post action=c.php>
<input type=text name=name2>Name<br>
<input type=submit value=go></form>";
?>
</body>
</html>

C.php
====

Code:
<?php
session_start();
?>

<?php
if(isset($_POST['form2'])){
   // Whatever fields you have in your form 1
   $_SESSION['name2'] = $_POST['name2'];
}
?>

<html>
<body>
<?php  print 'Your name is: ' . $_SESSION['name2']; ?>
<?php
echo "<form name=form2 method=post action=d.php>
<input type=text name=name2>Name<br>
<input type=submit value=go></form>";
?>
</body>
</html>
 
Ups, i forgot you want to pass your data to the forms' values!
Them... in this case you don't even need to use sessions... well.... depending on your forms/imputs quantity.
Just print the previous form posted data in the values of your actual form imputs.
You can also use hidden fields to store that posted data.
 
wait, so i can't even use session variables for it? i just wanna collect the data from all 3 forms/pages and put it in a db with mysql...
there's no way to do it with session variables, or anything easier?

thanks.
 
There are a way. Is using hidden form fields. But... forget that... sessions is the better way.
Please give a try in the code i've posted. Of course... add what is needed like, session_start()...
 
I did, I also added session_start() to A.php and it still does not work :( am i doing something wrong?

thanks.
 
What errors you get ?
Also use error_reporting(E_ALL); at first in your code, so you can see the errors in it.
In the sessions case, you can use print_r($_SESSION); to see all of your session variables.
 
There are no errors, the "name" variable is not displayed in B.php...
 
Scroll to the top where I posted my code, on B.php the line:
<?php print 'Your name is: ' . $_SESSION['name1']; ?>

the only thing it displays above form B is "Your name is:"
It does not show the name I typed in form A...

I also tried echo "$name1"; and that didnt work either.

:(
 
I know what's your problem...
Code:
<?php

if(isset($_POST['name_of_the_submit_forms_button'])){
    // Do the rest
}

?>
 
If you mean this than it still doesnt work :(

A.php
====

Code:
<?php
 session_start();
?>

<html>
<body>
<?php
echo "<form name=form1 method=post action=b.php>
<input type=text name=name>Name<br>
<input type=submit name=btn value=go></form>";
?>
</body>
</html>

B.php
====

Code:
<?php
 session_start();
?>

<?php
if(isset($_POST['btn'])){
   // Whatever fields you have in your form 1
   $_SESSION['name1'] = $_POST['name'];
}
?>

<html>
<body>
<?php  print 'Your name is: ' . $_SESSION['name1']; ?>
<?php
echo "<form name=form2 method=post action=c.php>
<input type=text name=name2>Name<br>
<input type=submit value=go></form>";
?>
</body>
</html>
 
Back
Top