Page 1 of 1

persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 11:59 am
by jaco
Hello everyone, I have been banging my head on this topic and reduced the problem to 2 short example html pages. What I am trying to do is make sure that the status of a checkbox is maintained across 2 pages and that variables are passed from one page to the other.

In the example code the first page (test.hp) allows the setting of a checkbox. When the submit button is pressed it calls the second (return-test.php) page where echo commands show if the variable has crossed to the page correctly. The submit button on this page returns to the 1st page.

What is happening is that when the checkbox is checked after being unchecked the variable does not get transfered to the 2nd page but the first cycle of check and uncheck works fine, Son of a gun...

Thanks fro any help, here is the code:

test.hp

Code: Select all

<?php
session_start();
 
echo "<html>";
echo "<head>";
echo "<body>";
 
 
 
if (!isset($_SESSION["count"]))
{
$_SESSION["count"]=0;
$ch_leg = "on";
$_SESSION["ch_leg"] = "on";
}
else
{
$_SESSION["count"]++;
}
echo "count =".$_SESSION["count"];
 
if ($_SESSION["count"] > 0)
$ch_leg = $_SESSION["ch_leg2"];
echo  "<br />";
echo "the value of ch_leg is = ".$ch_leg;
 
echo "<FORM NAME =\"form2\" METHOD =\"GET\" ACTION =\"return-test.php\">";
echo "<INPUT TYPE = \"Submit\" Name = \"results\" VALUE = \"Go to RESULTS page\">";
if ($ch_leg == "on")
echo "<Input type = \"Checkbox\" checked Name =\"ch_leg\" value =\"legacy_brand\">Legacy brand";
else
echo "<Input type = \"Checkbox\" Name =\"ch_leg\">Legacy brand";
echo "</FORM>";
 
if ($_SESSION["count"] > 0)
$_SESSION["ch_leg"] = $_GET["ch_leg"];
 
 
echo "</html>";
echo "</head>";
echo "</body>";
 
?>
and return.hp

Code: Select all

<?php
session_start();
 
 
$ch_leg2 =$_SESSION["ch_leg"];
$_SESSION["ch_leg2"] = $ch_leg2;
 
echo "<html>";
echo "<head>";
echo "<body>";
 
 echo "the value of session(ch_leg) is ".$_SESSION["ch_leg"];
echo  "<br />";
 echo "the value of ch_leg2 is ".$ch_leg2;
 echo  "<br />";
 echo "the value of SESSION(ch_leg2) is ".$_SESSION["ch_leg2"];
echo  "<br />";
 echo "count= ".$_SESSION["count"];
 
echo "<form action = \"test.php\">";
 echo  "Modifies number of pump data fields displayed ";
echo "<input type=\"submit\" value=\"Modify Display\">";
echo "</form>";
 
echo "</html>";
echo "</head>";
echo "</body>";
 
 
?>

Cheers,

Jacques

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 1:01 pm
by cpetercarter
Is there a special reason why you are passing the status of the checkbox in the session variables? The normal way is to POST from one page to the next, using 'hidden' fields if necessary.

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 1:13 pm
by jaco
No, I was just trying to implement what was suggested as the best solution. Do you think hidden fields is the better approach?

J

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 1:26 pm
by aceconcepts
When using forms the usual protocol is either to use a GET or POST method. Using the action attribute you can specify a destination path. From this path you can retrieve your POSTED variables (or you can GET them).

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 1:52 pm
by jaco
Sorry Ace, that's not clear to me, can you show a quick example?

J

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:15 pm
by jaco
I belive I have foound the solution. I needed to GET the var. in return.php.

the correct code is:

return-test.php

Code: Select all

<?php
session_start();
 
$ch_leg = $_GET["ch_leg"];
$_SESSION["ch_leg"] = $ch_leg;
 
echo "<html>";
echo "<head>";
echo "<body>";
 
 echo "the value of session(ch_leg) is ".$_SESSION["ch_leg"];
echo  "<br />";
 
 echo  "<br />";
 
echo  "<br />";
 echo "count= ".$_SESSION["count"];
 
echo "<form action = \"test.php\">";
 echo  "Modifies number of pump data fields displayed ";
echo "<input type=\"submit\" value=\"Modify Display\">";
echo "</form>";
 
echo "</html>";
echo "</head>";
echo "</body>";
 
 
?>
and test.php

Code: Select all

<?php
session_start();
echo "<html>";
echo "<head>";
echo "<body>";
 
 
if (!isset($_SESSION["count"]))
{
$_SESSION["count"]=0;
$ch_leg = "on";
$_SESSION["ch_leg"] = "on";
}
else  //session exists
{
$_SESSION["count"]++;
$ch_leg = $_SESSION["ch_leg"];
}
echo "count =".$_SESSION["count"];
 
echo  "<br />";
echo "the value of ch_leg is = ".$ch_leg;
echo "<FORM NAME =\"form2\" METHOD =\"GET\" ACTION =\"return-test.php\">";
 
echo "<INPUT TYPE = \"Submit\" Name = \"results\" VALUE = \"Go to RESULTS page\">";
if ($ch_leg == "on")
echo "<Input type = \"Checkbox\" checked Name =\"ch_leg\">Legacy brand";
else
echo "<Input type = \"Checkbox\" Name =\"ch_leg\">Legacy brand";
echo "</FORM>";
 
if ($_SESSION["count"] > 0)
$_SESSION["ch_leg"] = $_GET["ch_leg"];
 
 
 
echo "</html>";
echo "</head>";
echo "</body>";
 
?>
 
 

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:17 pm
by aceconcepts
Ok here's an example:

PAGE 1

Code: Select all

 
<form method="POST" action="page2.php">
  <input type="checkbox" name="check1" value="1" />
  <input type="submit" name="submit1" value="submit" />
</form>
 
PAGE 2

Code: Select all

 
if(isset($_POST['submit1'])){
  $check1 = $_POST['check1'];
}
 
Can you see how you may be bale to use this?

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:21 pm
by jaco
OK, so I gues session are not required?

J

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:32 pm
by aceconcepts
Well it depends how far you want to take things. What is your overall objective?

The example I gave simply POST a checkbox value to a targeted script which then assigns the POSTED value to a variable and that is it.

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:45 pm
by jaco
I need that when I return to page 1 the value of the checkbox is maintained.

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:47 pm
by aceconcepts
In this case a session variable would be quite advantageous. What you'll need to do is set the session var in page 2 when you get the POSTED checkbox value.

Once you've done this simply check the value of the session var in the first page and determine whether the checkbox should be "checked".

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:55 pm
by jaco
Also, the checkbox on test1.php has to be checked on the first run.

J

Re: persistence of sesson variables across 2 HTML pages

Posted: Tue Aug 11, 2009 2:57 pm
by aceconcepts
To do that all you'd have to do is check whether the session has been set or not e.g.

Code: Select all

 
if(!isset($_SESSION['check']))
{
  //make the checkbox default to "checked"
}
else
{
  //the session variable has already been set
}