#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Unknown php Notice


    Hello why do I get this Notice?
    Severity: Notice
    Message: Undefined index: hotel_id
    Line Number: 22
    PHP Code:
    21 $hotel_id="";
    22 $hotel_id $_POST['hotel_id'] ? $_POST['hotel_id'] : NULL
    23 $data = array(
            
    'name'        => 'hotel_id',
            
    'id'          => 'hotel_id',
            
    'value'       => $hotel_id,
            
    'class'       => 'text-box',
            
    'style'       => 'width:auto;'
    .
    .
    .
    ); 
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,847
    Rep Power
    6351
    $a = isset($_POST['thing']) ? $_POST['thing'] : '';
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by ManiacDan
    $a = isset($_POST['thing']) ? $_POST['thing'] : '';
    Strange! I thought $hotel_id=""; does the job.

    Thanks
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    Originally Posted by zxcvbnm
    Strange! I thought $hotel_id=""; does the job.
    No, that doesn't do anything in this case. The error message clearly says that this is an index problem: When the $_POST array doesn't have a "hotel_id" index, then $_POST['hotel_id'] will always generate a notice -- unless you use isset(). You could also check for array_key_exists().
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,847
    Rep Power
    6351
    You get the warning when you access an array key which does not exist. The setting of some other variable doesn't matter, you still need to call isset() before you check an array key you're not sure will exist.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo