Reputation: 115
I have a form in the homepage of a site, used as an include, that asks "Enter your zipcode", and with simple if else statements in the PHP file, it shows the preferred location (as provided by the client).
On submit, the location part simply uses the $_POST feature, and with some simple if else, locations are shown.
The problem i am having is carrying that value over to other pages. Once a zip code is entered and a result is shown, that result needs to carry over to other pages, but it doesn't I am having a hard time figuring that out.
/I've added a link -- */
http://www.subigya.com/stackoverflow/zipcode
Files:
http://www.subigya.com/stackoverflow/zipcode/zipcode.zip
I'm realizing some different problems now, and i can't get simply get sessions to run, i don't know why.
Upvotes: 2
Views: 1237
Reputation: 17169
POST and GET are only carried to the page you request it to. session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
If you do indeed have the zipcode as posted, then you can simply retrive it on the next page with $myzip = $_POST['zipcode'];
to do it with session you must:
session_start();
on the top of the pages and set the vairable w/ $_SESSION['zipcode'] = 'myzipcode';
or to retrive the value $myzip = $_SESSION['zipcode'];
Update w/ Your Current Code in process.php:
<?php
session_start();
if(count($_POST)>0)
{
$zip = $_POST['zipcode'];
$_SESSION['zipcode'] = $zip;
} else if(isset($_SESSION['zipcode']))
$zip = $_SESSION['zipcode'];
echo 'Current zipcode in session: ' . $_SESSION['zipcode'] . PHP_EOL;
if ($zip=="00000"){
echo'
<div id="result1" class="zipcodeResult">
<p><span class="locationName yellow">Location Name</span><br />
Address 1<br />
City, Texas 77025<br />
888.88.8888<br />
GM, <span class="underline">John Quest</span><br /><br />
<a href="#" target="_blank">Menu</a> | <a href="#" target="_blank">Catering</a> | <a href="#">Private Dining</a><br />
<a href="#" target="_blank">Take Out</a> | <a href="#">Map & Hours</a><br /><br />
<a href="search.php" class="backtoSearch">Change My Location</a></p>
</div>';
}else {
unset($_SESSION['zipcode']);
echo '
<div id="result6" class="zipcodeResult">
<p><span class="locationName yellow">Unrecognized Zip Code</span><br />
The zip code you have provided <br />
is out of range. <br /><br />
We primarily serve <br />
the greater Houston area <br />
in the state of Texas.<br /><br />
<a href="search.php" class="backtoSearch"> Click here to enter <br />
another zip code.</a></p>
</div>';
}
?>
Here is just a simple modification to your existing process.php. I added session, and basically stores the entered zipcode into session variable $_SESSION['zipcode'];
. Every time a user enters the zipcode in process.php it'll display what is placed into the session var zipcode. Since you have it in an iframe, to see if the session is working simply just click on the link that you have echo out, which should point to itself. If you were using GET or POST, the Session var zipcode would not show up, but it would if you are using Session. Hope this helps.
Upvotes: 3
Reputation: 15571
If you need the zipcode only for time being and you don't need it to be persisted for the user, then you can either use session to store it for the entire session.
You can also use GET (adding the zipcode in the query string) if you move from one page to another. This is suitable, if you need to carry it along for a minimal no of pages and you do not have too many things to be carried along.
POST is another way of doing it and in that case you shall have to use cross page posting.
If you feel that the data should be persisted in the user's preference for future use, the best will be to put it in the user-profile and use it.
Using session or keeping it in the user-profile has one benefit that you can use it on any page across the site. You do not need to explicitly carry it around. But for POST and GET, you need to take the data to the other page explicitly. This might need some management.
Upvotes: 0
Reputation:
Using PHP there are multiple ways of transferring information form one page to another
1) Sessions (http://no2.php.net/manual/en/book.session.php)
On one page:
$_SESSION['someKey'] = 'someValue';`
On another page:
echo $_SESSION['someKey'];
For sessions to work you need to run session_start();
at the top of both pages (and indeed every page you want the session to exist).
2) POST (http://www.php.net/manual/en/reserved.variables.post.php)
3) GET (http://www.php.net/manual/en/reserved.variables.get.php)
Upvotes: 0