php session form data retreival

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

Post Reply
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

php session form data retreival

Post by jaco »

Hello everyone, thanks for any help you could provide I am spinnin' my wheels.

I am trying to use the SESSION var. to hold the contants of a form variable over other php pages. It seems no matter what I do I cannot display the form var. on a second php page or retrieve it on the initial page when I use the back button.

test-display.php

Code: Select all

 
<?php
//This call leither opens a new session or finds an existing onw
session_start(); 
 
require 'header.html';
 
echo "<FORM NAME =\"form2\" METHOD =\"GET\" ACTION =\"test-pump.php\">";
echo "<INPUT TYPE = \"Submit\" Name = \"Submit1\" VALUE = \"Modify display\">";
echo "Choose flow unit: ";
echo "<select name=\"flow_metric\">";
echo "<option value=\"USgpm\">USgpm</option>";
if ($flow_metric == "l/s") echo "<option SELECTED value=\"l/s\">l/s</option>";
else echo "<option value=\"l/s\">l/s</option>";
if ($flow_metric == "l/min") echo "<option SELECTED value=\"l/min\">l/min</option>";
else echo "<option value=\"l/min\">l/min</option>";
if ($flow_metric == "m3/h") echo "<option SELECTED value=\"m3/h\">m3/h</option>";
else echo "<option value=\"m3/h\">m3/h</option>";
 
echo  "</select>";
 
$flow_metric = $_GET['flow_metric'];
 
$_SESSION['flow_metric'] = $flow_metric;
 
$_SESSION['color']='red'; 
 
echo "</FORM>";
echo "Metric flow value is ".$_SESSION['flow_metric'];
 
// standard html web page footer
require 'footer.html';
 
?>
and test-pump.php

Code: Select all

<?php
//This call leither opens a new session or finds an existing onw
session_start(); 
 
// standard html web header
require 'header.html';
 
echo "Metric flow value is ".$_SESSION['flow_metric'];
 
echo "Our color value is ".$_SESSION['color']; 
 
 
// standard html web page footer
require 'footer.html';
 
 
?>
 

You can find the page here:

http://www.lightmypump.com/pumpdatabase ... isplay.php

Cheers,

Jacques
Last edited by Benjamin on Tue Jun 30, 2009 12:04 am, edited 1 time in total.
Reason: Added [code=php] tags.
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: php session form data retreival

Post by BornForCode »

Did you check if you have something in the $_GET['flow_metric']?
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: php session form data retreival

Post by jaco »

Indeed, that's the problem. I can't figure out how to get data into it.

J
BornForCode
Forum Contributor
Posts: 147
Joined: Mon Feb 11, 2008 1:56 am

Re: php session form data retreival

Post by BornForCode »

Anyway, this is a working code and i think you should use it because is cool :mrgreen: :drunk:

Code: Select all

 
<?php 
session_start();
$products = array("USgpm","1/s","1/m","m3/h");
 
?>
 
<form method="GET" action="test_pump.php">
    Choose flow metric: 
    <select name="flow_metric">     
        <?php
        foreach ($products as $value) {
            echo '<option value="'.$value.'" '.(($value == $_SESSION['flow_metric'])?'selected>':'>').$value.'</option>';
        }
        ?>
    </select>
    <input type="submit" value="Submit"/>
</form>
 
Add this lines in the test_pump.php or where you need to make the assignement:

Code: Select all

 
if(isset($_GET['flow_metric']))
$_SESSION['flow_metric'] = $_GET['flow_metric'];
 
One of you problems is that you have code after form and once you press submit no code is executed so the following lines never get to be executed:

Code: Select all

 
$flow_metric = $_GET['flow_metric'];
 
$_SESSION['flow_metric'] = $flow_metric;
 
$_SESSION['color']='red'; 
 
echo "</FORM>";
echo "Metric flow value is ".$_SESSION['flow_metric'];
 
Last edited by Benjamin on Tue Jun 30, 2009 12:04 am, edited 1 time in total.
Reason: Changed code type from text to php.
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: php session form data retreival

Post by jaco »

Yes, this works very well, thank you.

Jacques
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: php session form data retreival

Post by jaco »

I need to call a 3rd page and come back to the 2nd and not loose the values.

I tried using the code you mention on the 3rd page but I still loose the values when I come back to the 2nd page.

sheesh...
Post Reply