"Notice undefined index" for an existing element in $form array Basically This is my Array

 array( 
        [product_id] =&gt; Array
        ( 
         [#type] =&gt; hidden
         [#value] =&gt; 39 
        )         
      )


and I am trying to get the value which is '39'

$cartId = $form['product_id']['#value'];


and yes I get the value without any issues, but in my site it gives me a notice saying "Notice: Undefined index: product_id in  ..."

This is my entire code

function MyModule_form_alter( &amp;$form, $form_state, $form_id )
{
    $cartId = $form['product_id']['#value'];
    print_r ($form['#value']);
    if ($form_id == 'commerce_cart_add_to_cart_form_'.$cartId)
    { 
        $form['add_to_wishlist']['#weight']= 49;
    }   
}


any ideas how to fix this? because I couldn't figure the cause of it as it works well!
Your function is called for all forms in your Drupal. $cartId = $form['product_id']['#value']; is called outside if, so it's also executed on all forms. And most of them does not have $form['product_id'] defined at all. Move that part inside if block.