persistence of sesson variables across 2 HTML 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

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

persistence of sesson variables across 2 HTML pages

Post 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
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: persistence of sesson variables across 2 HTML pages

Post 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.
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: persistence of sesson variables across 2 HTML pages

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: persistence of sesson variables across 2 HTML pages

Post 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).
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: persistence of sesson variables across 2 HTML pages

Post by jaco »

Sorry Ace, that's not clear to me, can you show a quick example?

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

Re: persistence of sesson variables across 2 HTML pages

Post 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>";
 
?>
 
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: persistence of sesson variables across 2 HTML pages

Post 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?
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: persistence of sesson variables across 2 HTML pages

Post by jaco »

OK, so I gues session are not required?

J
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: persistence of sesson variables across 2 HTML pages

Post 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.
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: persistence of sesson variables across 2 HTML pages

Post by jaco »

I need that when I return to page 1 the value of the checkbox is maintained.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: persistence of sesson variables across 2 HTML pages

Post 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".
jaco
Forum Newbie
Posts: 13
Joined: Mon Jun 29, 2009 6:40 pm

Re: persistence of sesson variables across 2 HTML pages

Post by jaco »

Also, the checkbox on test1.php has to be checked on the first run.

J
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: persistence of sesson variables across 2 HTML pages

Post 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
}
 
Post Reply