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 4 of 4
  1. #1
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Very simple but won't work

    I'm just doing a test to see if I can pass the variable "mike" from page to page. I can echo it out on 2.php but not on 3.php. What am I doing wrong?

    PHP Code:
    <html>
    <
    form action="2.php" method="post">
    <
    input type="hidden" name="id" value="mike">
    <
    input type="submit">
    </
    form>
    </
    html
    PHP Code:
    <html><form action="3.php" method="post">
    <?
    //$id=$_POST['id'];
    echo $id;
    ?>
    <input type="submit">
    </form>
    </html>
    PHP Code:
    <html>
    <form>
    <?
    //$id=$_POST['id'];
    echo $id;
    echo 
    "hi";
    ?>
    </form>
    </html>

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    It won't be on page 3 unless you put it in a hidden field on 2.php. If you're going by multiple pages I think you'd enjoy using sessions instead of hidden fields.

    Good luck;

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    values not in input tags (or selects or textareas) within a form won't get passed when the form is submitted. Your second page doesn't contain any inputs other than the submit, so nothing will be passed.
    One way of doing what you want it to use 'sessions'. This is a method whereby you can preserve information on the server between page loads for a particular visitor.
    for example:
    page1.php
    PHP Code:
    <?php
    session_start
    ();
    echo 
    $_SESSION['test']; //will either be blank, or give a 'notice' of undeclared variable
    $_SESSION['test']='hello';
    echo 
    $_SESSION['test']; //will echo 'hello'
    ?>
    <a href='/page2.php'>Page 2</a>
    page2.php
    PHP Code:
    <?php
    session_start
    ();
    echo 
    $_SESSION['test']; //will echo hello
    unset($_SESSION['test']);
    ?>
    <a href="/page3.php">Page 3</a>
    page3.php:
    PHP Code:
    <?php
    //no session_start() this time
    echo $_SESSION['test']; //session not starte, no nothing/notice again
    ?>

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,037
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Ahhh... you have the thread where an id is not carrying over between pages.

    In addition to the lack of a hidden input field in the form, you also need to access the value received from the previous form page using $_POST['id']; (Using just $id would work if register_globals are on, but the hidden input field is still needed in either case.) If you are checking out the bogus information someone posted in that other thread about variables carrying over between pages, well that was bogus, web servers don't work that way.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

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