Page 1 of 1

values of arrays across the website

Posted: Sun Apr 19, 2009 1:36 pm
by straightman
Hello everybody.

Does anybody how to solve this difficulty?

I have a search form of hotels and countries with dropdown menus from which you select some elements. I get the tabulated results alright after submission. These tabulated results have per every row a link for further detail into the hotel. That works fine too. So, once I am in the detail page, I have to provide a link back to the tabulated results for the case the visitor wants to check other hotels too, to see them in detail, just like he did before. The problem is here: I find no way to send the values through that link back to the results page. Of course, I use sessions but it makes no difference. The fact is that the page that displays the tabulated results got its input from arrays coming from the dropdown menus, but obviously, that is gone in the detail page so, because there are no user defined superglobals I cannot resend that value to the script that would display the tabulated results.

thank you

Re: values of arrays across the website

Posted: Sun Apr 19, 2009 2:38 pm
by MasterBeta
Why not use an if statement to determine if the session is set, and if it is, use it. Otherwise use the post data. This may cause a problem if you want the user to be able to do another search, but you could use session_destroy() somewhere too.
Something like

Code: Select all

if(isset($_SESSION[value])) $id = $_SESSION[value];
else $id = $_POST[value];

Re: values of arrays across the website

Posted: Sun Apr 19, 2009 3:04 pm
by straightman
Thank you for your help. I will try that, but..: in the code you write we expect that the $_POST will get the value that was originally created after submission, however, i basically do that in my link, and it does not contain any in it.

In the url link i do include the $_POST variable, which, being a superglobal, it should be reachable from anywhere. but when I hover on the link i can see that it is empty , that is, the detail page does not retain the value, and therefore, cannot resend it.

echo
"<TD width=125 height=35>" ."<a href='". "../fromsearchform.php?$d=". urlencode($_POST['activity']). "&$c= " . urlencode($_POST['country']). "'>Resultslist</a>". "</TD>";

So, the link Resultslist sends me to the php script that processes the results from the search form, which works well when it comes directly from the searchform because this one passes it the values alright, but when i get to this page from other page, it doesnt.

Re: values of arrays across the website

Posted: Sun Apr 19, 2009 3:59 pm
by MasterBeta
The $_POST data will only be set upon form submission so you NEED to use $_SESSION to store the variables.
The page where the form is submitted to needs to have your $_SESSION set by the $_POST

Code: Select all

$_SESSION['activity'] = $_POST['activity'];
$_SESSION['country'] = $_POST['country'];
Then call it like this

Code: Select all

echo 
"<TD width=125 height=35>" ."<a href='". "../fromsearchform.php?$d=". urlencode($_SESSION['activity']). "&$c= " . urlencode($_SESSION['country']). "'>Resultslist</a>". "</TD>";
Also, don't forget that you must start your session on the first line of each page
<?php
session_start();
?>

Also consider that using the $_GET method may open security issues.

Re: values of arrays across the website

Posted: Wed Apr 22, 2009 6:09 pm
by straightman
Thank you very much heartedly for the time you have dedicated. It sounds very convincing what you write and it has been good learning too. I will try it then. Will let you know.