SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problem passing form value

    Hi, I'm trying to do a simple case of displaying a submitted value but I'm getting a blank output.

    This is my form:

    <form action="name.php" method ="post">

    <input type ="text" size ="45" name="username">
    <input type ="submit" value="submit">

    </form>


    This is the output page:

    <?php
    echo("your name is $username");
    ?>

    and the output is just:

    your name is


    What am I doing wrong?

  2. #2
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $username isnt assigned anything..

    try to put this in the code..
    $username=$_POST["username"];

    This will assign the value submitted from the form ($_POST) to the variable $username

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still get a blank.

    Shouldn't I be getting the follwing url?: name.php?username=cosmic_bird

    the url I get is just name.php

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry tdob, it actually does work now, thanks!

  5. #5
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you use the POST method on a form, the URL is whatever is set in the action attribute fo the form, and the named fields on the form are accessible via the $_POST array.

    When you use the GET method on the form, the named filelds are append to the action attribute and can thuse be seen. In this case, the variables are accessible via the $_GET array.

    For diagnostic purposes, at the top of your name.php file, put the following lines of code


    PHP Code:
      echo 'POST Varaibles <br />';
      
    print_r ($_POST);
      
      echo 
    'GET Varaibles <br />';
       
    print_r ($_GET); 
    This will display all the varaibles passed to name.php from either the ULR or vai the POST.

    Hope this helps

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Abilene, TX
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason your URL is just name.php is because you are posting data, rather than getting data. However, your PHP code is correct. If you are getting blank data, something is wrong on your server end. Try checking your PHP configs. Also, what version of PHP are you running?
    Reg

    "I don't care how many beers you've had...
    Get your chickens out of my lawn!"

  7. #7
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I am a newbie, but this is generally the type of code I have with forms..

    PHP Code:
    <?php if(isset($_POST["username"])) { 
    $username=$_POST['username'];
    echo 
    'Your username is '$username''; }
    ?>
    Again, I am new to this as well, and I am not at home to check if it works, so its from the top of my head. The echo statement may be a little wrong, but the post part should work

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Dublin
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    everything seems to be working ok now, thanks 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
  •