Results 1 to 3 of 3

Thread: Notice: Undefined index: id on line 27

  1. #1
    Join Date
    Jul 2008
    Location
    Serbia
    Posts
    47
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Notice: Undefined index: id on line 27

    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.


    Code:
    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
    Last edited by bokanegro; 06-06-2010 at 04:43 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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']; }
    Last edited by djr33; 06-06-2010 at 07:44 PM. Reason: typo
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    bokanegro (06-06-2010)

  4. #3
    Join Date
    Jul 2008
    Location
    Serbia
    Posts
    47
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    Hi, djr 33

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

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
  •