Problem with $_GETting a value and assigning it to a variable

Discussion in 'PHP' started by dankenpo, Jun 15, 2007.

  1. #1
    I'm having a problem with this. Let's say, I want to get a user's name in a simple form, the action being a new page that will use the name (newpage.php) and the method being "GET."

    
    <form action="newpage.php" method="get">
    What is your name? 
    <input type="text" name="Uname" maxlength="25" size="30" /><br />
    <input type="submit" value="Customize" />
    </form>
    
    Code (markup):
    Now, in newpage.php, we want to use their name in a greeting. This is what I tried:

    
    <?php
    $theirName = $_GET['Uname'];
    echo "Hello ".$theirName."!";
    ?>
    
    Code (markup):
    This hasn't been working for me. However, what will work is if I simply use:
    
    <?php echo $Uname; ?>
    
    Code (markup):
    In the above example, I have not defined any variable by using the $_GET array, I simply am able to get the value by throwing a $ in front of it. Why won't it assign the value to the variable I specify? Why has it become a variable without any $_GET or actions on my part? I know I am a beginner, this confuses me.

    Anyone's help would be greatly appreciated!
     
    dankenpo, Jun 15, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    What version of php are you using ?

    The only reason for registering superglobals is having register_globals in your ini, which is an incredibly bad idea and it can make it hard to program.....
     
    krakjoe, Jun 15, 2007 IP
    dankenpo likes this.
  3. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I am using PHP 4.1.1 I believe to develop locally on my machine (it is what came with the PHPTriad installation) and my web server uses PHP5, but the same error occurred on both. I apologize, I am still learning, could you explain your response some more please?

    I am a C++ programmer who is learning PHP, and in many ways PHP is much easier, but in several ways it confuses me. I'm mostly learning by trial and error and reading other people's scripts.
     
    dankenpo, Jun 15, 2007 IP
  4. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can anyone explain to me what krakjoe meant? I am learning all of this stuff. . .
     
    dankenpo, Jun 15, 2007 IP
  5. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #5
    "register_globals On" in php.ini means that all variables like $_POST['var1'], $_GET['var2'] are made variables in your script, like $var1, $var2

    krackjoe is right, using that setting is very bad idea, you should set it off or update your php to latest (register_globals is off on php > 4.2)

    read this http://www.php.net/manual/en/security.globals.php
     
    gibex, Jun 15, 2007 IP
  6. dankenpo

    dankenpo Guest

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks gibex and thanks again krakjoe!
     
    dankenpo, Jun 16, 2007 IP