Page 1 of 6

how to keep a submitted $variable alive over multiple pages

Posted: Wed Jul 09, 2003 6:26 am
by Marco van Wijngaarden
I'm working with PHP for my first time, so forgive me, but i'm kinda new here! I've worked my way through books and now i'm struggling with the following;
------------------------------------------------------------------------------------
--- Page1.php ---
here's a input box called "name"
------------------------------------------------------------------------------------
--- Page2.php ---
here's the result from "name" printed with the following string:

Code: Select all

<?php
print ("$name<BR>\n");
?>
------------------------------------------------------------------------------------
--- Page3.php ---

this page is supposed to take care of sending out an email with the value of "$name" but here it seems that the value has been lost cos i'm receiving a blank email!!
===============================================

Now i am wondering, what will be the best to do here, since i need more values to pass when i finaly get this to work... will an Array stay 'alive' over multiple pages or is there any other way? If anyone could give me some understandable directions for this newbie that would be great! Thanks!

Posted: Wed Jul 09, 2003 6:33 am
by []InTeR[]
Try using session's.
http://www.php.net/sessios

Something (on page 2) like..
session_register("name");

Posted: Wed Jul 09, 2003 9:06 am
by McGruff
Sounds like a register globals problem. Try $_POST['name'] (or $_GET['name'] if that's the form method) and see the sticky thread on register globals.

I think you've got register globals off which is good (more secure) and hence you have to refer to the superglobal POST or GET arrays. There's also $_REQUEST but it's better to be more specific, again for security reasons.

Posted: Wed Jul 09, 2003 9:10 am
by Marco van Wijngaarden
okay, so now i have the following codes on my pages:

-PAGE1-

Code: Select all

<form name="klantgegevens" method="post" action="stap2.php">
<?php
session_start();
if (nameform) {
session_register("naam");
}
?>
<input type="text" name="naam">

<input type="submit" name="Submit" value="Verzenden">
<input type="hidden" name="nameform" value="TRUE">
-PAGE2-

Code: Select all

<form name="data" method="post" action="stap3.php">
<?php
session_start();
session_register("naam");
echo $naam;
?>
<p> 
    <input type="submit" name="Submit" value="naar stap 3">
    <input type="hidden" name="checkform" value="TRUE">
-PAGE3-

Code: Select all

<?php
<?php
session_start();
session_register("naam");
echo $naam; 
?>
But it still returns me a blanc page on -PAGE3-
What am i doing wrong here??
Thanks in advance for your help! :oops:

Posted: Wed Jul 09, 2003 9:29 am
by Owe Blomqvist
Ok.
On stap2.php.
Insert the following inside the form tag:

Code: Select all

<Input type="hidden" name="naam" value="<?=$naam?>">
That should do it.
Regards,
Owe Blomqvist

Posted: Wed Jul 09, 2003 9:47 am
by Marco van Wijngaarden
Okay, but now it's just one $variable to handle, but what if i have about 100.... isn't there another way to get this going?

Thanks anyway, i still will try if this will work!
Marco

Posted: Wed Jul 09, 2003 10:03 am
by Owe Blomqvist
You can either do it like above or do:

Code: Select all

<?php
session_start();
$_SESSION['myform'] = array( 
                                               'var1'    =>    $var1,
                                               'var2'    =>    $var2,
                                               'var3'    =>    $var3,
                                               'var4'    =>    $var4,
                                               'var5'    =>    $var5,
                                               'var6'    =>    $var6
                                            );
?>
...And so on.
I guess there are more ways to do it, but this and the <input type="hidden"> way, are the ways i do it.
You can then access the values by using for exmple, $_SESSION['myform']['var1']

Posted: Wed Jul 09, 2003 11:43 am
by m3rajk
if you want the names to be the same as what the form name is, then explode($_POST); will work.

Posted: Wed Jul 09, 2003 2:36 pm
by Marco van Wijngaarden
Okay guys, thanks for your input! I'll do some experiments with your info tomorrow and see where that will take me! Once again, i appreciate your help! Marco

Posted: Thu Jul 10, 2003 5:31 am
by Marco van Wijngaarden
So here i am with the next thing i don't understand yet! I have tried the following

---stap1.php---

Code: Select all

<?php
session_start();
	if (nameform) {
	$_SESSION['klantgegevens'] = array( 
   	'naam'          =>    $naam, 
                'adres'          =>    $adres, 
                'postcode'     =>    $postcode, 
                'woonplaats'  =>    $woonplaats, 
                'telefoon'       =>    $telefoon, 
                'email'    	    =>    $email,
	'afleveren'     =>    $afleveren
); 
}
?>
?>
---stap2.php---

Code: Select all

<?php
session_start(); 
session_register("klantgegevens");
print $_SESSION['klantgegevens']['naam'];
print $_SESSION['klantgegevens']['telefoon'];
?>
this is NOT working, so what am i doing wrong here? I can't find any detailed info on printing the variables from an Array...

Posted: Thu Jul 10, 2003 7:05 am
by Owe Blomqvist
Well, Since $naam etc, is not yet set on page 1, I suggest you move the array to stap2.php.
You should fill your array after you submitted the form.

stap2.php:

Code: Select all

<?php
session_start(); 
   if (nameform) { 
        $_SESSION['klantgegevens'] = array( 
            'naam'           =>    $naam, 
            'adres'           =>    $adres, 
            'postcode'      =>    $postcode, 
            'woonplaats'   =>    $woonplaats, 
            'telefoon'       =>    $telefoon, 
            'email'           =>    $email, 
            'afleveren'     =>    $afleveren 
        ); 
   }
?>
Regards,
Owe Blomqvist

Posted: Mon Jul 14, 2003 2:50 am
by Marco van Wijngaarden
thanks for your response, but still something seems to be wrong since i don't get any output on "stap3.php" or "popup.htm"

---stap1.php---

Code: Select all

<?php
<form name="klantgegevens" method="post" action="stap2.php">
<P>Bedrijf <input type="text" name="bedrijf" size="50"></P>
<P>Naam <input type="text" name="naam" size="50"></P>
<P>Adres <input type="text" name="adres" size="50"></P>
<P>Postcode <input type="text" name="postcode" size="7" maxlength="7"></P>
<P>Plaats <input type="text" name="woonplaats" size="50"></P>
<P>Telefoon <input type="text" name="telefoon" size="50"></P>
<P>Email <input type="text" name="email" size="50"></P>
<p>Afleveradres <textarea name="afleveren" rows="3" cols="50"></textarea></p>
<p>
<input type="submit" name="Submit" value="Naar stap 2">
</p>
</form>
?>
---stap2.php---

Code: Select all

<?php
<form name="data" method="post" action="stap3.php">
<?php
session_start();
$_SESSION['klantgegevens'] = array( 
'bedrijf'     =>    $bedrijf,
'naam'        =>    $naam, 
'adres'       =>    $adres, 
'postcode'    =>    $postcode, 
'woonplaats'  =>    $woonplaats, 
'telefoon'    =>    $telefoon, 
'email'	      =>    $email,
'afleveren'   =>    $afleveren
);
?> 
<P>regel1<input type="text" name="regel1"></P>
<P>regel2<input type="text" name="regel2"></P>
<P>regel3<input type="text" name="regel3"></P>
<P>regel4<input type="text" name="regel4"></P>
<P>regel5<input type="text" name="regel5"></P>
<P>regel6<input type="text" name="regel6"></P>
<P>regel7<input type="text" name="regel7"></P>
<P>regel8<input type="text" name="regel8"></P>
<P>regel9<input type="text" name="regel9"></P>

<input type="button" name="Submit2" value="Vorige" onClick="MM_callJS('history.back(1)')">
<input type="submit" name="Submit" value="Naar stap 3">
<input type="hidden" name="checked2" value="TRUE">
</FORM>
?>
---stap3.php---

Code: Select all

<?php
<?php
session_start(); 
$_SESSION['data'] = array( 
'regel1'      =>    $regel1,
'regel2'      =>    $regel2, 
'regel3'      =>    $regel3, 
'regel4'      =>    $regel4, 
'regel5'      =>    $regel5, 
'regel6'      =>    $regel6, 
'regel7'      =>    $regel7,
'regel8'      =>    $regel8,
'regel9'      =>    $regel9
); 
session_register("klantgegevens"); 
print $_SESSION['klantgegevens']['naam']; 
print $_SESSION['klantgegevens']['bedrijf'];
?>
?>
---popup.htm---

this popup window should put the value of "regel1" and "regel2" from the array "data", into the text fields.

Code: Select all

<?php
<?php 
session_start(); 
session_register("data"); 
?> 
<p> 
<input type="button" name="Submit2" value="Sluiten" onClick="MM_callJS('window.close()')">
<Input type="text" name="regel1" value="<?=$_SESSION['data']['regel1']?>">
<Input type="text" name="regel2" value="<?=$_SESSION['data']['regel2']?>">
?>

Posted: Mon Jul 14, 2003 12:52 pm
by m3rajk
i don'tknow if it'll help, but until step 4, my multiple page form kept step. it keeps everything i toss to cookies
http://people.brandeis.edu/~m3rajk/JMT/ ... signup.txt

Posted: Tue Jul 15, 2003 1:43 am
by Marco van Wijngaarden
Thanks for your input, i have been going over your code, but since i am just a newbie to this PHP programming, it doesn't learn me what i might be doing wrong on my code! Thanks anyway!

Marco

Posted: Tue Jul 15, 2003 3:39 am
by Heavy
The following is the old way of registering session variables:
session_register("data");
You should not use it. A fact is that session_register() and $_SESSION does not like each other.
There is really only one thing you should do before using session vars, and that is to start the session:

Code: Select all

<?php
session_start();
$_SESSION['data'] = 'some text';
echo $_SESSION['data'];
?>
That's it. Sessions are that simple today.

If you like to remove a session var you could go:

Code: Select all

<?php
unset($_SESSION['data']);
?>
Session vars can even contain the data of an object, if its class is declared in every page that uses it.