SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Real Newbie: Sending variables with URL

    Ok - I'm experimenting with a few different things in PHP ... and I've come up against sneding variables with the URL.

    Here's the line in my script:
    print "$choice";
    and here's what I'm typing as my URL (on a local server):

    http://localhost/filelist.php?choice=mine
    Why is it not printing the word "mine" out in my browser?!?!?

    Thanks for your help!

  2. #2
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without seeing more of the code, you might try something like this instead:

    <?php echo $_get[$choice];?>

  3. #3
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm afraid that is my script at the moment bvarvel

    I thought I'd start right at the beginning and work my way up - at least that way if something goes wrong then I know where to start looking.

    At the moment, all I want to do is print out the contents of the variable which has been added to my URL, but at the moment it's not working.

    I tried your idea, which I thought might have solved it ... but unfortunately it didn't

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    I think Brian is on the right track.
    There's a small typo in the code. Try:
    PHP Code:
    <?php echo $_GET['choice'];?>
    Also add this line at the top of your script.
    PHP Code:
    error_reporting(E_ALL); 
    That will display all error that the PHP engine is giving. You will get an error if you try Brian's code with the error level set to E_ALL

    HTH

    -Helge

  5. #5
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think I've got it fixed

    It seems that I needed to actually assign the value of the GET variable to a different variable in the PHP script itself, if that makes any sense. Something like this:

    $decision = $_GET['choice'];
    echo $decision;
    It seems that I can't use the variable straight away!

  6. #6
    Web Genius
    Join Date
    Nov 2001
    Location
    Canada
    Posts
    708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're close [img]images/smilies/smile.gif[/img]

    http://localhost/filelist.php?choice=mine

    Then for the PHP pge filelist.php

    <?
    // Get variable from URL and assign it to choice variable!
    $choice = $_GET['choice'];

    //Output the variable
    print ($choice);
    ?>

    Cheers [img]images/smilies/thumbs_up.gif[/img]

  7. #7
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops.. my bad. Newborn in the house keeping me awake at nights.. and look what happens. Sorry.

    you shouldn't need to declare it first... you should be able to access it in-line.. but regardless.. if that got it.. that's al that's important.

  8. #8
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Quote Originally Posted by simontheak
    Think I've got it fixed

    It seems that I needed to actually assign the value of the GET variable to a different variable in the PHP script itself, if that makes any sense. Something like this:

    It seems that I can't use the variable straight away!
    It should not be necessary to assign the variable into another before you echo it out. Strange.

    What PHP version do you use?

    -Helge

  9. #9
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, maybe you tried outputting it in a string and used a wrong syntax in which the variable couldn't be parsed? Try
    PHP Code:
    echo 'You have chosen: '.$_GET['choice'].'! Isn\'t that cool?'

  10. #10
    Resident Boozehound simontheak's Avatar
    Join Date
    Jun 2003
    Location
    _root.europe.uk.london
    Posts
    965
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much guys!


Bookmarks

Posting Permissions

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