Jump to content

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


edwardda

Recommended Posts

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

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>";
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.