...

View Full Version : Linked page with different information depending on where it was linked from?



mssteph
11-08-2010, 11:42 PM
I'm new to PHP, so bear with me, sorry. I have a page that has a list of cities on it. What I want is for the user to be able to click on any city and be taken to a page where the name of that city shows up on the page. Can this be done with only one linked page? Maybe with a simple print or echo?

Thanks for any input!

Keleth
11-08-2010, 11:48 PM
The best and easiest way to do it (if you have no db), is to pass a get variable, such as this for the link:

page.php?city=New_York_City

Then on page.php, it can be accessed via $_GET['city'], and boom, New_York_City. Of course, if there's a db involved, you should pass the key.

mssteph
11-09-2010, 03:30 PM
The best and easiest way to do it (if you have no db), is to pass a get variable, such as this for the link:

page.php?city=New_York_City

Then on page.php, it can be accessed via $_GET['city'], and boom, New_York_City. Of course, if there's a db involved, you should pass the key.

Thank you so much for your help! I tried this on a couple of servers and on my local and it didn't work. I did some research on GET and every solution seems to include a form. Is there a way for it to work without needing to be passed through by a form, in other words, just by a link? Here's how I have it set up:



home.php:

<p><a href="city.php?city=New_York_City">New York</a></p>

<p><a href="city.php?city=Boston">Boston</a></p>

city.php:

<p>Welcome to <?php $_GET['city'] ?> !</p>



Thanks for any help.

Keleth
11-09-2010, 03:36 PM
There's no need for a form, what you have is almost right.


<?php $_GET['city'] ?>

What that does is get the value of 'city' from the GET array... then do nothing with it. Any content you want appearing on the page needs to be echo'ed.

mssteph
11-09-2010, 03:50 PM
There's no need for a form, what you have is almost right.


<?php $_GET['city'] ?>

What that does is get the value of 'city' from the GET array... then do nothing with it. Any content you want appearing on the page needs to be echo'ed.

Thank you! I tried that right after I posted and still nothing:



<p>Welcome to <?php $_GET['city'] ?> <?php echo $city ?>!</p>


I must be doing something wrong but I can't see it.

mssteph
11-09-2010, 03:52 PM
Got it!!!!

For anyone else, solution is:



<p>Welcome to <?php echo $_GET['city'] ?>!</p>


Thank you Keleth so much for your help!! :)

bngari
11-09-2010, 03:54 PM
Thank you so much for your help! I tried this on a couple of servers and on my local and it didn't work. I did some research on GET and every solution seems to include a form. Is there a way for it to work without needing to be passed through by a form, in other words, just by a link? Here's how I have it set up:



home.php:

<p><a href="city.php?city=New_York_City">New York</a></p>

<p><a href="city.php?city=Boston">Boston</a></p>

city.php:

<p>Welcome to <?php $_GET['city'] ?> !</p>



Thanks for any help.

You have it all okay, I'll just add a few pointers...

- In the home.php file, it would be better for you to rewrite the links containing cities with spaces like New York City as

<p><a href="city.php?city=<?php echo urlencode("New York City"); ?>">New York</a></p>
instead of
<p><a href="city.php?city=New_York_City">New York</a></p>

- In the city.php file, modify the code that displays the information to look like

<p>Welcome to <?php echo urldecode($_GET['city']); ?> !</p>


Thats it, your scripts will work just fine.

mssteph
11-09-2010, 04:32 PM
You have it all okay, I'll just add a few pointers...

- In the home.php file, it would be better for you to rewrite the links containing cities with spaces like New York City as

<p><a href="city.php?city=<?php echo urlencode("New York City"); ?>">New York</a></p>
instead of
<p><a href="city.php?city=New_York_City">New York</a></p>

- In the city.php file, modify the code that displays the information to look like

<p>Welcome to <?php echo urldecode($_GET['city']); ?> !</p>


Thats it, your scripts will work just fine.

This is wonderful, thank you. I will use this.

Thanks guys. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum