how to keep a submitted $variable alive over multiple pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Marco van Wijngaarden wrote:Heavy gee, thanks! 8O i'll see if this will work for me! but i also got another idea to go around this trouble (cos what if my host WILL decide to upgrade after reading my complaining for instance?) The idea i have is to use cookies... that's also new for me, but what do you think?
You only need one cookie, and that is the one that keeps track of your session ID and PHP does that for you so you yourself do not have to concern about cookies at all.

Do sessions or no storage at all (ie use posting hidden files).

You do realize that I wrote that for you and tested it using PHP4.2.3 ???

In other words, you don't need to panic, but I strongly recommend your host to upgrade. If they decide to do so, the only things you may want to change (you don't have to, but I guess PHP5 won't support the old way though) in your code is to get rid of session_register() and change :

* $HTTP_SESSION_VARS to $_SESSION.
* $HTTP_POST_VARS to $_POST

Then you are up to date with the new globals style.

NO COOKIES!
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

ok. i've got an idea. mentioned it in msn. and i thought it looked sorta like german there. (i've taken german but ich spreche bisschen Deutsche)

Code: Select all

<?php

$step=$_POST['step'];

if($step==1){
	/* error check step 1 */
}elseif($step==2){
	/* error chec step 2 */
}

if($step==2){
 prepage($step);
 startpage();
 errorcheck($step, $error_messages, $error);
 page2();
}else($step==1){
 prepage($step);
 startpage();
 errorcheck($step, $error_messages, $error);
 page1();
}

function prepage($step){ # cookies and headers go here
	/* set cookies and headers--if they change do NOT change witht he step, you can remove that, otherwise passing it gives you the way to make it customized to the step */
}

function startpage(){ # the begining of each page sould be the same, so place it here

}

function errorcheck($step, $error_messages, $error){ # this displays the errors of the page so they know what they need to fix
 if($error){
	if($step==1){}
	elseif($step==2){}
 }
}

function page1(){ # body of page 1
?> this is a small set of info to be collected for something
<form>
<table><input type="hidden" name="step" value="2">
<tr><td>name</td><td><input type="text" name="name"></td></tr>
</table>
</form>
<?php
}

function page2(){ # body of page 2
?>
this is step 2.
this is what you entered in step 1:
<form><input type="hidden" name="step" value="1">
<table>
<tr><td>name</td><td><input type="text" name="name" readonly></td></tr>
</table>
<input type="submit" value="fix a mistake">
</form>
please enter step 2 information
<!-- step 2 form -->
<?php
}
?>
from my understanding, if you don't name the forms, then only step should be dependant upon which submit is pushed, this may solve your problem.... at least if you only have 3 steps
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Do we trust the browser to have these form defaults: ?
* method="POST"
* action="<?php echo basename($_SERVER['PHP_SELF'])?>"

???
I think you should set it in the HTML source.

Further, as long as Marco uses that old version, $_POST doesn't exist.

...and I'm not very sure that code is easy to read.
I always post things to the same URL I'm at, but as someone seems to be learning the basics, we shouldn't suggest something that looks a bit messy. :roll:
User avatar
Marco van Wijngaarden
Forum Commoner
Posts: 59
Joined: Mon Jul 07, 2003 6:48 am
Location: The Netherlands

Post by Marco van Wijngaarden »

hey folks... i am now working with the post from Heavy and i'll see if i will be able to expand it... just trying stuff... so i'll get back with my results! Indeed it works, but i ran into a couple of issues so i have to see if i will be able to solve those before i start bothering you again! :)

ohh and FYI: it's dutch, the language, and yes it looks a bit like german!
User avatar
Marco van Wijngaarden
Forum Commoner
Posts: 59
Joined: Mon Jul 07, 2003 6:48 am
Location: The Netherlands

Post by Marco van Wijngaarden »

on that last post from Heavy;

I guess it does makes sence to me actually! Thing is i do understand most of the code i see, only when i try to write mine i don't remember or forget details on the code... but as long as i know how to call certain things i am okay here!

On your comment regarding $_Post
In my case (dealing with php 4.0.6) would that be $POST_VARS ???
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

No. $POST_VARS is wrong.

It's here:
viewtopic.php?p=50370#50370

Also, look at he code here:
viewtopic.php?p=50358#50358

EDIT: If it is so that you first encountered sessions from your book and the book is written for your PHP version, you should read about it there.
User avatar
Marco van Wijngaarden
Forum Commoner
Posts: 59
Joined: Mon Jul 07, 2003 6:48 am
Location: The Netherlands

Post by Marco van Wijngaarden »

Now on the code from Heavy

It does work, but it have some issues... as i mentioned before...

In short this is what happens:

---Step2.php---

here's the result from the submitted variables from Step1.php


---Step3.php---

shows the result from the submitted variables from Step1.php and should also show those submitted from Step2.php however this is what i returns even while i've submitted a variable

Code: Select all

Array
(
    [klantgegevens] => Array
        (
            [bedrijf] => value of "bedrijf"
            [naam] => value of "naam"
            [adres] => 
            [postcode] => 
            [woonplaats] => 
            [telefoon] => 
            [email] => 
            [afleveren] => 
        )

    [data] => 
)
Then on Step4.php everything comes out as it should!
But the thing is, i have a link on step3.php that should make a window popup which includes some variables...

On that (popup)page i put the following script:

Code: Select all

<?php
session_start(); 
echo "<pre>"; 
print_r ($HTTP_SESSION_VARS); 
echo "</pre>";
?>
which doesn't work, but probably because i still have a session running on step3.php, since that window is still open!
Now... i assume i could send those variables through the URL... but isn't it better to call these from the session??

Then... i am having trouble with filling hiddenfields on that popup window... can't seem to find any examples in the old style of programming either....

i've tried this:

Code: Select all

<input type="text" name="regel1" value="<?php print ("$HTTP_SESSION_VARS['regel1']"); ?>" onLoad='interactive1(this.value)'>
but it doesn't work!

Anyway... i am getting there, but i guess i still need some tips and tricks if anyone could be so kind!?
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

Then on Step4.php everything comes out as it should!
If all variables are there on step 4, then the error in step 3 must be something like printing the session before setting the new data into it.

Code executes from top with direction down line by line.

Code: Select all

<?php
$HTTP_SESSION_VARS['varname'] = $HTTP_POST_VARS['varname'];
echo $HTTP_SESSION_VARS['varname'];
?>
...will print content of $HTTP_SESSION_VARS['varname'].

Code: Select all

<?php
echo $HTTP_SESSION_VARS['varname'];
$HTTP_SESSION_VARS['varname'] = $HTTP_POST_VARS['varname'];
?>
...will also print content of $HTTP_SESSION_VARS['varname'], but there is no content since the content is set too late.

Try that theory on step 3.


which doesn't work, but probably because i still have a session running on step3.php, since that window is still open!
That's got nothing to do with it. When the page is loaded into the browser PHP is sleeping. You can have multiple frames in a windows using sessions without problem. You must have done something else wrong.

Go check that <?php comes absolutely first in the popup window, so that session really gets started.
but isn't it better to call these from the session??
Absolutely better.

Code: Select all

<input type="text" name="regel1" value="<?php print ("$HTTP_SESSION_VARS['regel1']");?>" onLoad='interactive1(this.value)'>
that will not print what you want.
Do like this instead:

Code: Select all

<input type="text" name="regel1" value="<?php print ($HTTP_SESSION_VARS['regel1']);?>" onLoad='interactive1(this.value)'>
If you don't understand why, read this carefully.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

heavy: that wasnt't meant to be working, but to give an outline that might be useful since it would include error checking. it's obvious that marco knows html so i figured i could cut out a bunch of things in making an outline.

i forgot about the 4.0.6 v 4.1+ issue ($_POST[] v $HTTP_POST_VARS[])

like i said, it looked similar to german to me. i think dutch and german have a common root language
User avatar
Marco van Wijngaarden
Forum Commoner
Posts: 59
Joined: Mon Jul 07, 2003 6:48 am
Location: The Netherlands

Post by Marco van Wijngaarden »

Okay folks, nearly about to plot my code here to finnish this toppic in the forum... but before i do, i still have one thing...

Now i have my variables available anywhere (feels good by the way :D ) so that is sort of what i intended when i started this topic! I mentioned i have several steps for input by the end-users... so it would be nice if users could also go back to change things, but that doesn't seem to work!
(meaning javascript:history.back(1) doesn't work)

Now i could insert an extra page which will put the variable into a textfield which will enable editing and in the end overwrite the previous submitted value...
Also i think i could make a script to clear the submitted values and then re-direct to the page to set those variables again....
but isn't there another way to return to previous page?
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

It might be related to the fact that you are POSTING data between the forms.

Change the method to GET instead:

<form name="blabla" action="stepX.php" method="GET">

do so in all forms

and replace all the $HTTP_POST_VARS with $HTTP_GET_VARS




BTW, I never use the javascript you showed. I'm not saying that it is wrong, because I don't know if it is... :oops: But here's what I do:

<input type="button" onclick="history.go(-1)" value="Go back">

or

<a href="javascript:history.go(-1)">Go back</a>
User avatar
Marco van Wijngaarden
Forum Commoner
Posts: 59
Joined: Mon Jul 07, 2003 6:48 am
Location: The Netherlands

Post by Marco van Wijngaarden »

once again Heavy strikes... thanks! That worked!

Now i'm struggling with a "few steps in one" file... the following script returns me a blanc page, even while "[step] => 1" has been set on previous page already! so what seems to be wrong here??

Code: Select all

<?php
$step=$HTTP_POST_VARS['step'];

if($step==3){
session_start();
session_register('result');
startpage();
page3();
}elseif($step==2){
session_start();
session_register('mail');
startpage();
page2();
}elseif($step==1){
session_start();
session_register('number');
startpage();
page1();
}

function page1(){
?>
<title>Ordernummer</title>
<font face="Arial, Helvetica, sans-serif" size="2"><b>ordernummer toekennen</b></font>
<form>
<input type="hidden" name="step" value="2">
<input type="submit" value="OK">
</form>
<?php
$HTTP_SESSION_VARS['ordernummer'] = $ordernummer;

}

function page2(){
?>
<title>Bestelling Mailen</title>
<font face="Arial, Helvetica, sans-serif" size="2"><b>tabel per e-mail verzenden</b></font>
<form>
<input type="hidden" name="step" value="3">
<input type="submit" value="OK">
</form>
<?php

}

function page3(){
?>
<title>Resultaat Verzenden</title>
<font face="Arial, Helvetica, sans-serif" size="2"><b>resultaatpagina toont:</b> 
    </font> 
    <ul>
      <li><font face="Arial, Helvetica, sans-serif" size="2"> faktuurbedrag</font></li>
      <li><font face="Arial, Helvetica, sans-serif" size="2">ordernummer</font></li>
      <li><font face="Arial, Helvetica, sans-serif" size="2">bankrekeningnummer 
        Wellnessprintshop </font></li>
    </ul>
<?php

}

function startpage(){
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body bgcolor="#FFFFFF" text="#000000">
</body>
<?php
}
?>
User avatar
Marco van Wijngaarden
Forum Commoner
Posts: 59
Joined: Mon Jul 07, 2003 6:48 am
Location: The Netherlands

Post by Marco van Wijngaarden »

i guess there was an error... i've already solved this by the following...

1. the previous page holds a hidden field named "step" and value "1"

on this script i've added a line at the top, so now it works fine! Sorry to bother you!

Code: Select all

<?php 
$HTTP_POST_VARS['step'] = $step;
$step=$HTTP_POST_VARS['step'];
?>
User avatar
Heavy
Forum Contributor
Posts: 478
Joined: Sun Sep 22, 2002 7:36 am
Location: Viksjöfors, Hälsingland, Sweden
Contact:

Post by Heavy »

What does that elseif statement execute if $step is not set?
Answer: nothing...

Do a switch statement instead, it looks cleaner:

Code: Select all

<?php
session_start();

switch(intval($HTTP_POST_VARS['step'])){
    case 3:
        session_register('result');
        startpage();
        page3();
        break;

    case 2:
        session_start();
        session_register('mail');
        startpage();
        page2();
        break;

    case 1:
    default:
        session_register('number');
        startpage();
        page1();
        break;

}

// The "default" branch is what gets executed if none of the cases match.
// Ie, if intval($HTTP_POST_VARS['step']) gets evaluated to 0, zero.

function page1(){
?>
<title>Ordernummer</title>
<font face="Arial, Helvetica, sans-serif" size="2"><b>ordernummer toekennen</b></font>
<form>
<input type="hidden" name="step" value="2">
<input type="submit" value="OK">
</form>
<?php
$HTTP_SESSION_VARS['ordernummer'] = $ordernummer;
// and what is this??? ordernummer is not registered in the session and $ordernummer is undefined.
//I guess you didn't post it all to this forum.


}

function page2(){
?>
<title>Bestelling Mailen</title>
<font face="Arial, Helvetica, sans-serif" size="2"><b>tabel per e-mail verzenden</b></font>
<form>
<input type="hidden" name="step" value="3">
<input type="submit" value="OK">
</form>
<?php

}

function page3(){
?>
<title>Resultaat Verzenden</title>
<font face="Arial, Helvetica, sans-serif" size="2"><b>resultaatpagina toont:</b> 
    </font> 
    <ul>
      <li><font face="Arial, Helvetica, sans-serif" size="2"> faktuurbedrag</font></li>
      <li><font face="Arial, Helvetica, sans-serif" size="2">ordernummer</font></li>
      <li><font face="Arial, Helvetica, sans-serif" size="2">bankrekeningnummer 
        Wellnessprintshop </font></li>
    </ul>
<?php

}

function startpage(){
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body bgcolor="#FFFFFF" text="#000000">
</body>
<?php
}
?>
There is broken HTML in this script. If you execute startpage() first, you output </body> before the forms. That's wrong.

It seems like much processing code is missing.
You never take care of the data that is posted.
Guess you didn't post it all...

May I suggest you fill in the <form> tags with proper method and action parameters?

<form method="POST" action="<?php echo basename($PHP_SELF)?>">

(The $PHP_SELF variable refers to the running scripts full name, including path. In your PHP version I think it should be $PHP_SELF, but I may be wrong. In latest PHP versions, it is called $_SERVER['PHP_SELF'])
User avatar
Marco van Wijngaarden
Forum Commoner
Posts: 59
Joined: Mon Jul 07, 2003 6:48 am
Location: The Netherlands

Post by Marco van Wijngaarden »

Well... what i am intending to do here is this:

1. retrieve an $ordernumber (still to be implemented) which would be just a simple counter variable....

2. after the variable $ordernumber is set the variable $step will be changed by $step++; so the script will turn to step 2 by itself...
(the form and button is now there just for testing perpose!)

3. on step2 (this still has to be set as well) i will set up $body which will be a html message with variables from $HTTP_SESSION_VARS to be send through a mail($To, $Subject, $Body) function

4. again the script itself should go to the next step by $step++;

5. the next step will display the result... which will be that the mail has been send and also it display's some variables such as $ordernumber and $faktuurbedrag as reminder for the end user before they'd close the window!

Now, i seem to have trouble to get these variables displayed:

Code: Select all

<font face="Arial, Helvetica, sans-serif" size="2">Het totaal bedrag van uw bestelling is 
    &euro; <?php printf ("%01.2f", "$faktuurbedrag");?>
    </font>

?>
Thanks on your comment regarding the </body>, that should be moved into each step, thanks!
Post Reply