RE: [PHP] session variables - help
| From: | Ford, Mike | Date: | Fri, 14 Aug 2009 14:52:20 +0000 |
| Subject: | RE: [PHP] session variables - help | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296792@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Allen McCabe [mailto:allenmccabe@gmail.com]
> Sent: 14 August 2009 06:58
>
> Here is some more complete code:
>
> [code = order_process.php]
>
> <?php
> session_start();
> // POST ALL $_POST VALUES, CREATE AS VARIABLES IN SESSION
> foreach($_POST as $k=>$v) {
> $_SESSION[$k]=$v;
> }
>
> $thisPage="AFY"; //NAVIGATION PURPOSES
> include("afyshows.php"); //CONTAINS ARRAYS FOR SHOW ENTITIES;
> POPULATES
> ORDER FORM
> ?>
>
> . . .
>
> </p><form name="update" action="update_order.php"
> method="post" >
> <!-- HIDDEN FORM VALUES FOR SESSION PURPOSES -->
Er wait, no! Sessions and hidden form fields are generally alternative solutions to the same problem
-- you shouldn't be putting the same values both in the session and in hidden form fields. In
this case, I'm beginning to suspect that the hidden fields are the better solution, but there
is a certain amount of personal preference in this.
> <input type="hidden" name="School" id="School"
> value="<?php
> $_SESSION['School']; ?>" />
> <input type="hidden" name="Grade" id="Grade"
> value="<?php
> $_SESSION['Grade']; ?>" />
> <input type="hidden" name="Address" id="Address"
> value="<?php
> $_SESSION['Address']; ?>" />
> <input type="hidden" name="City" id="City"
> value="<?php
> $_SESSION['City'];
> ?>" />
> <input type="hidden" name="State" id="State"
> value="<?php
> $_SESSION['State']; ?>" />
> <input type="hidden" name="Zip" id="Zip" size="9"
> value="<?php
> $_SESSION['Zip']; ?>" />
> <input type="hidden" name="Contact" id="Contact"
> value="<?php
> $_SESSION['Contact']; ?>" />
> <input type="hidden" name="Phone" id="Phone"
> value="<?php
> $_SESSION['Phone']; ?>" />
> <input type="hidden" name="Fax" id="Fax" value="<?php
> $_SESSION['Fax']; ?>"
> />
> <input type="hidden" name="Email" id="Email"
> value="<?php
> $_SESSION['Email']; ?>" />
> . . .
>
> <?php
>
> function findTotalCost($b, $c) {
> $total = $b * $c;
> return $total;
> }
>
> function writeResultRow($a, $b, $c, $d, $e, $f) {
> if($a != '') {
> echo "\n<tr>\n\t";
> echo
> "<td'>".$b."</td><td>".$c."</td><td>".$d."</td>";
> echo
> "<td>".$e."</td><td> </td><td><input
> type='text'
> value='".$a."'
> name='".$a."' id='".$a."' size='2'
> /></td><td>=</td><td>\$".$f."</td>";
> echo "</tr>";
> }
> }
>
> //SETS $Total_show_01 to PRICE * QUANTITY
> //FORMATS TOTAL
> //IF A QUANTITY IS ENTERED, WRITES THE ROW WITH CURRENT VARIABLES
> $Total_show_01 = findTotalCost($shows['show_01']['price'],
> $_SESSION['show_01_qty']);
> $Total_show_01_fmtd = number_format($Total_show_01, 2, '.', '');
> writeResultRow($_SESSION['show_01_qty'],
> $shows['show_01']['title'],
> $shows['show_01']['date'], $shows['show_01']['time'],
> $shows['show_01']['price'],$Total_show_01_fmtd);
>
> //ABOVE LINES REPEATED FOR ALL 38 ENTITIES (show_01 to show_38)
>
> ?>
> . . .
>
> <input name="updates" id="updates" type="submit"
> value="Update"/>
>
> [/code]
If I'm reading what you want to do correctly, it seems to me there are two obvious approaches
to this:
(i) Have a single form which posts back to itself, showing all the show information and requested
quantities and calculated result fields (such as total cost); initially, this will have the
calculated fields not displaying anything, and these will be (re)populated at each Update. Using
this method, all your values are contained solely within the $_POST array.
(ii) Have your initial form post to the process form, which then also posts to itself on Update.
This process form will have visible fields only for values which can be changed, but *must* then
contain hidden fields for all the other values which were originally passed in the $_POST array.
This arrangement means that the process form always receives a full complement of values in the
$_POST array -- either from the original form, or from hidden fields posted back to itself.
This is all just coming off the top of my head, and I'm sure there are improvements/other
solutions to be offered. Hope this will give you some things to think about, and maybe a pointer or
two towards a satisfactory solution.
Cheers!
Mike
--
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,
Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 812 4730
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm