#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3

    Question Undefined variable that "is" defined?


    Hi all,

    I've got a page (blah.html) that checks if a value is set for a select box. If it's not, then the select box is displayed with options. If it is set, then some other stuff is displayed.

    First time into this page, $var1 is not set (as expected), so the select box is shown.

    However, when either "This" or "That" is selected (i.e. $var1 then has a value) and the form is submitted, the expected result should be that the "Some other html" should be displayed. It's not. When I echo $var1, it always shows "" instead of "1" or "2" when either is selected. And, the php error says "Undefined variable" after a selection is made and the form is submitted.

    Code:
    <? if (!isset($var1)) { ?>
    <form action="blah.html" method="post">
    
    <select name="var1">
    <option value="">-Select One-
    <option value="1">This
    <option value="2">That
    </select>
    </form>
    
    <? } else { ?
    Some other html.
    <? } ?>
    If "This" or "That" is selected on the first loading of the page, and then submitted, what would cause $var1 to *not* be set and thus not falling into the } else { where is should be after the submit?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I don't see $var1 defined anywhere in the code. I hope you're not using the ancient register_globals?

    What does your browser tell you about the POST request? Open the developer tools and check the form parameters.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by we5inelgr
    Hi all,

    First time into this page, $var1 is not set (as expected), so the select box is shown.
    What do you think sets $var1 ?

    You could get it like this
    PHP Code:
    $var1 $_POST['var1']; 
    However, when either "This" or "That" is selected (i.e. $var1 then has a value)
    Not until you assign it a value.

    Unless you are using really old code that puts all your $_GET and $_POST variables into php variables, and that's not a good idea.

    This code works, but NOT recommended
    PHP Code:
    <html>
    <head>
    </head>
    <body><?php
     
    if (!isset($_POST['var1'])) {
     
    ?>
    <form action="blah.php" method="post">

    <select name="var1">
    <option value="">-Select One-
    <option value="1">This
    <option value="2">That
    </select>
    <input type="submit" value="submit">
    </form>

    <?php } else { ?>
    Some other html.
    <?php ?>
    </body>
    </html>
    Also, if you want your ACTION file to do stuff, then it needs to be of a type that the server can parse, like php, not html.

    You need to use proper php script tags too.
    PHP Code:
    <?  ?>
    are shortcut tags and NOT supported everywhere.
    You should use
    PHP Code:
    <?php  ?>
    to mark up php code.

    Bottom line. You are using a VERY old tutorial. You need to find something more up to date.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Thanks for the replies guys.

    $var1 is coming from the select box.

    When the page first gets loaded, there is nothing selected and therefore no value.

    When either option "This" or "That" is selected, and the form is submitted, I can see other variables on the resulting page...but for some reason the value from the select dropdown doesn't get set.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    If one of the items is selected from the dropdown, and the form submitted,

    this fails to identify that $var1 has a value:
    if (!isset($var1))

    but this works:
    if (!isset($_POST['var1']))



    Perhaps a newer version of PHP is running on the server since this code was last used successfully?

    In other words, is (!isset($var1)) deprecated?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by we5inelgr
    is (!isset($var1)) deprecated?
    Yes.

    http://php.net/manual/en/security.globals.php

IMN logo majestic logo threadwatch logo seochat tools logo