Jump to content


Photo

Undefined index: error in C:\wamp\www\tourprice.php on line 32


  • Please log in to reply
1 reply to this topic

#1 edwardda

edwardda

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 10 May 2013 - 06:59 PM

Can someone please  help > im new to php and trying to complete code for  a tour price calculator and keep getting the following error message

 

 Undefined index: error in C:\wamp\www\tourprice.php on line 32

 

line 32 reads

 

32   if ($_GET['error'] == "notnumeric"){

 33      echo "<p class=\"error\">*** Error! One or more fields was left blank or contained a non-numeric character.</p>";
34     }
 ?>

 

any help would be greatly appreciated



#2 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,255 posts

Posted 10 May 2013 - 08:35 PM

if $_GET['error'] is optional (may or may not exist), you need to first test if it is set before you test the value in it.

if(isset($_GET['error']) && $_GET['error'] == "notnumeric"){

if it's not optional and you expect it to always be present, you should probably troubleshoot why it isn't present.

 

 

 

if you are are testing that variable against more than about one value/error message, you should reduce the amount of logic needed to only one set and lookup the value using an array that maps the incoming text string to the actual message - 

$error_lookup['notnumeric'] = "*** Error! One or more fields was left blank or contained a non-numeric character.";
// add other error names/error messages here

$error = isset($_GET['error']) ? strtolower(trim($_GET['error'])) : '';
if(!empty($error)){
    if(isset($error_lookup[$error])){
        echo "<p class='error'>$error_lookup[$error]</p>";
    } else {
        // bad value
        echo "<p class='error'>An undefined error ($error) was triggered.</p>";
    }
}

multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com