Forum Moderators: coopster
eg, I have a page that has several columns, I have existing code that generates numbers from 3 to 9 and when they are clicked I want a session variable set to that value.
Then retrieve that value when I go to the new page with the reload hyperlink.
I can't use a form or button on this page - for asthetic reasons it has to be a hyperlink.
Ideally, I would like to press the hyperlink and it set's the value of the session variable and then reloads the page. but this seems to be impossible with PHP.
What am I doing wrong?
Here is my code:
<p>Columns:
<?php
for($i=3; $i<9; $i++)
{
echo "<a href='javascript:SetCols($i);'>$i</a>\n";
}?>
<font size="-2"><a href="links_main.php">RELOAD</a></p>
I know that the javascript must be wrong (as the section is never called) so what can I do to fix it?
Sometimes it's hard to see the forest for the trees.
In short, my test pages use simple counters and page1 and page2 are the same except for obvious differences:
<?php
if (!session_id()) session_start();
if (isset($_SESSION['Count']))
{
$_SESSION['Count']++;
}
else
{
$_SESSION['Count']=1;
}
if (isset($_SESSION['Page1']))
{
$_SESSION['Page1']++;
}
else
{
$_SESSION['Page1']=1;
}
?>
<html>
<head>
<title>Test Page Sessions</title>
</head>
<body>
<?php echo "Count: ".$_SESSION['Count']."<br>\n";?>
<?php echo "Page1: ".$_SESSION['Page1']."<br>\n";?>
<?php echo "Page2: ".$_SESSION['Page2']."<br>\n";?>
<a href="page1.php">page1</a> :
<a href="page2.php">page2</a> :
<a href="pager.php?r=1">RESET1</a> :
<a href="pager.php?r=2">RESET2</a>
</html>
<?php
if (!session_id()) session_start();
if (isset($_REQUEST['r']))
{
if(1==$_REQUEST['r'])
{
$_SESSION['Page1']=0;
}
elseif(2==$_REQUEST['r'])
{
$_SESSION['Page2']=0;
}
}
$_SESSION['Page1']--;
header("Location: page1.php");
?> Thanks for your speedy reply. It really helped!
It is good practice to keep code and HTML separate as much as possible.
Changing the SESSION_ID value is kind of working against yourself as a security thing. The whole idea around SESSIONS as I understand them, is having a tracker which is unpredictable. Setting that to a predictable number, you might as well use a cookie reference, unless you have some other reason for using the SESSIONS, (for instance you are just experimenting and what to see how much you can mess with things, which is a time honored method of learning any new language).
The "best way" is a difficult question, for me, because I don't really know your final need. What it appears you are doing is trying to find a comfortable way of "Maintaining State".
Here are some webpages which might give you some ideas, and help you understand what you are working with.
[phpbuilder.com...]
[wdvl.com...]
[zend.com...]
Hope that helps,