Log in

View Full Version : Resolved Notice: Undefined index: id on line 27



bokanegro
06-05-2010, 11:32 PM
Hello,

Can anyone help me with fixing this notice. I don't wont to disable errors with php.ini, I wish to fix this.

I'm working on localhost, php version is 5.3.0:

Red is error line.



if ($_SERVER['REQUEST_METHOD'] == 'POST' ) {
$id = $_POST['id'];

switch ($_POST['action']) {
case 'add':
$cart->addItem($id, $products[$id]->name, $_POST['qty'], $products[$id]->price, $products[$id]->weight, $products[$id]->url);
break;

case 'remove':
$cart->removeItem($id);
break;

case 'empty':
$cart->removeAll();
session_destroy();
break;
}
}

Thanks for you time

djr33
06-06-2010, 03:03 AM
The PHP is correct, but the submitted data is not. That means that "id" was not sent from a form. Either your form does not have this field or you did not enter anything in it.

You can default to an empty string (blank value) like this:
$id = isset($_POST['id'])?$_POST['id']:'';

($id = was_id_sent?TRUE[sent_id]:FALSE[blank])

Or, if you don't like the short syntax, just use ifs: $id = ''; if (isset($_POST['id'])) { $id = $_POST['id']; }

bokanegro
06-06-2010, 08:48 AM
Hi, djr 33

Thanks for your help, it's sooo easy when you have php knowledge. Anyway thanks once again.