SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: isset problem

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    isset problem

    I am having a problem with isset. I have a short test program shown below where I have one variable "addr".

    It doesn't matter if I type something into the form and submit it, or leave the form blank, the results are the same. It reports the variable is set in both cases.

    I added an echo statement under the isset code to verify the php file was receiving the correct data.

    This is causing me a problem is a much larger program.

    Why is this happening?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title></title>
    </head>
    <body>
    
    <form action="test2.php" method="post">
    Addr: <input type="text" name="addr" />
    <input type="submit" value="Give it a try" />
    </form>
    
    </body>
    </html>

    Code:
    <?php
    //file test2.php
    
    $addr=null;
    
    $addr=$_POST['addr'];
    
    if (isset($addr)) {echo "addr is set";}
    if (!isset($addr)) {echo "addr is not set";}
    
    ?>
    Lawrence

  2. #2
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try using empty() instead of isset()
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  3. #3
    SitePoint Wizard spence_noodle's Avatar
    Join Date
    Jan 2004
    Location
    uk, Leeds (area)
    Posts
    1,264
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Get rid of this line:

    PHP Code:
    $addr=null
    And change it to empty() like what blain has said above. Isset is really for varibles that are set with either a number or letter, as for empty is more general.
    "Don't you just love it when you solve a programming bug only to create another."

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spence_noodle View Post
    Get rid of this line:

    PHP Code:
    $addr=null
    And change it to empty() like what blain has said above. Isset is really for varibles that are set with either a number or letter, as for empty is more general.
    Thank you very much.

    "empty" works.

    Lawrence

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    isset() != empty()
    Your variable is set, because when the form is submitted, it's sent to the page.
    But it can be set and empty.
    You have to check both.

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2005
    Location
    Southern Nevada, USA
    Posts
    150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tripy View Post
    isset() != empty()
    Your variable is set, because when the form is submitted, it's sent to the page.
    But it can be set and empty.
    You have to check both.
    Thank you. So much to learn and so little time.......

    Lawrence


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
  •