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

    Whats the problem with the link and button

    Just wrote a quick script to see if a variable is being passed. This works ok when using the button, but the variable won't pass using the link. Any idea why?

    PHP Code:
    <html>
    <
    form action="2.php" method="post">
    <
    input type="hidden" name="id" value="mike">
    <
    input type="submit">
    </
    form>
    </
    html
    PHP Code:
    2.php
    <html><form action="3.php" method="post">
    <?
    echo "<input type=hidden name=id value='$id'>";
    echo 
    "Variable $id has passed";
    ?>
    <br>
    <input type="submit">
    </form>
    </html>
    PHP Code:
    <html><form action="4.php" method="post">
    <?
    echo "<input type=hidden name=id value='$id'>";
    echo 
    "Variable $id has passed twice";
    ?>
    <br>
    <a href="4.php">Click here to see if variable will pass </a>
    <br>
    <br>
    <input type="submit"></a>
    </form>
    </html>
    PHP Code:
    <html>
    <form>
    <?
    $id
    =$_POST['id'];
    echo 
    "<input type=hidden name=id value='$id'>";
    if (isset(
    $_POST['id'])){
    echo 
    "YES! variable $id has pass to a third page via a Link";
    } else {
    echo 
    "Nope variable did not pass";
    }
    ?>

    </form>
    </html>

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    A link in a form is just a link. Clicking it does not cause the form to be submitted.

    To pass a value this way, there are some different choices -

    1) You can pass it as part of the action= URL. It will be available on the next page as a $_GET variable (you can pass parameters on the action = URL using the GET method and pass form fields using the POST method in the same form.)

    2) If you only want to use a link without this being part of a form and pass parameters as part of the URL, you can create a href = link similar to what you have, append the parameters, and they will be available on the next page as $_GET variables.

    3) You can cause a form to be submitted using a link by using an onclick event to submit the form. This seems to be what you are trying by placing the link within your form.
    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.

  • #3
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your response. Would you be willing to show me a brief example of each of the 3 ways.

    Thanks again

  • #4
    mwm
    mwm is offline
    Regular Coder
    Join Date
    Oct 2005
    Posts
    174
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?

    Thank You

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    This method has one problem ... anyone can see $id by looking at the source code.
    If $id is a sensitive or secret value, you should be using sessions instead.

    PHP Code:
    <html>
    <
    form action="2.php" method="post">
    <
    input type="hidden" name="id" value="mike">
    <
    input type="submit">
    </
    form>
    </
    html
    PHP Code:
    2.php
    <?php
    $id
    =$_REQUEST['id'];
    ?>
    <html><form action="3.php" method="post">
    <input type="hidden" name="id" value="<?=$id?>"><br>
    Varible to pass is:  <?=$id?><br>
    <input type="submit">
    </form>
    </html>
    PHP Code:
    <?php
    $id
    =$_REQUEST['id'];
    ?>
    <html><form action="4.php" method="post">
    <input type="hidden" name="id" value="<?=$id?>"><br>
    Varible to pass is:  <?=$id?><br>
    <input type="submit"></a>
    </form>
    </html>
    PHP Code:
    <html>
    <?php
    $id
    =$_REQUEST['id'];
    if (isset(
    $id)){
    echo 
    "YES! variable $id has passed to a third page via a Link";
    } else {
    echo 
    "Nope variable did not pass";
    }
    ?>
    </html>
    Last edited by mlseim; 11-28-2006 at 06:01 PM.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,129
    Thanks
    2
    Thanked 328 Times in 320 Posts
    Anyway, here are working examples of the three different ways mentioned -
    PHP Code:
    <?php
    // method 1, form code -
    $id "mike";
    ?>
    <form action="f1b.php?id=<?php echo $id;?>" method="post"> 
    Enter some name: <input type="text" name="name"> 
    <input type="submit"> 
    </form>
    PHP Code:
    <?php
    // method 1, action= code, f1b.php -
    echo "The id is: {$_GET['id']}<br />";
    echo 
    "The entered name is: {$_POST['name']}";
    ?>
    PHP Code:
    <?php
    // method 2 code -
    $id "mike";
    echo 
    "<a href=\"f2b.php?id=$id\">Click here to see if variable will pass </a>"
    ?>
    PHP Code:
    <?php
    // method 2, target code, f2b.php -
    echo "The id is: {$_GET['id']}";
    ?>
    PHP Code:
    <?php
    // method 3, form code -
    $id "mike";
    ?>
    <form name="userform" action="f3b.php" method="post"> 
    <input type="hidden" name="id" value="<?php echo $id;?>"> 
    Enter some name: <input type="text" name="name">
    </form> 
    <a href=# onclick="document.userform.submit()">Click to Submit</a>
    Note: Because the onclick refers to the form by its name="..." name, you can put the link in the form or outside the form...

    PHP Code:
    <?php
    // method 3, action= code, f3b.php -
    echo "The id is: {$_POST['id']}<br />";
    echo 
    "The entered name is: {$_POST['name']}";
    ?>
    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
    •