Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2008
    Location
    MD, USA
    Posts
    65
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Linked page with different information depending on where it was linked from?

    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!

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    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.

  • Users who have thanked Keleth for this post:

    mssteph (11-09-2010)

  • #3
    New Coder
    Join Date
    Jul 2008
    Location
    MD, USA
    Posts
    65
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    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:

    Code:
    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.

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    There's no need for a form, what you have is almost right.

    Code:
    <?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.

  • Users who have thanked Keleth for this post:

    mssteph (11-09-2010)

  • #5
    New Coder
    Join Date
    Jul 2008
    Location
    MD, USA
    Posts
    65
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Keleth View Post
    There's no need for a form, what you have is almost right.

    Code:
    <?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:

    Code:
    <p>Welcome to <?php $_GET['city'] ?> <?php echo $city ?>!</p>
    I must be doing something wrong but I can't see it.

  • #6
    New Coder
    Join Date
    Jul 2008
    Location
    MD, USA
    Posts
    65
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Got it!!!!

    For anyone else, solution is:

    Code:
    <p>Welcome to <?php echo $_GET['city'] ?>!</p>
    Thank you Keleth so much for your help!!

  • #7
    New Coder
    Join Date
    Nov 2010
    Location
    Kenya
    Posts
    42
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by mssteph View Post
    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:

    Code:
    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.

  • Users who have thanked bngari for this post:

    mssteph (11-09-2010)

  • #8
    New Coder
    Join Date
    Jul 2008
    Location
    MD, USA
    Posts
    65
    Thanks
    16
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bngari View Post
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •