views:

25

answers:

2

I would like to retrieve a cck field "field_info" in my form alter to insert into another table when user is submitting. This doesn't seem to work.

//mymodule_form_alter() implemented
       function mymodule_form_mysubmit{

            $test = $form['field_info']['#value'];
        //insert stuff code
    }

Is there any mistake in the code?

+2  A: 

You say module_form_alter() is implemented, but just to confirm, you need to have the following in it:

$form['#submit'][] = 'mymodule_form_mysubmit';

Assuming you do, to get the value of field_info, your submit function should look like:

function mymodule_form_mysubmit($form, &$form_state) {
  $test = $form_state['values']['field_info'][0]['value'];
}

$form_state contains the current state of the form being submitted. CCK always assumes that there could be multiple values for a field, so it always puts things in an array (hence ['field_info'][0]).

Mark Trapp
A: 

I found the solution

        $test = $form['field_info'][0]['#value'];
charan
The $form array is not safe, as it contains the structure of the form, and not necessarily the current form's values. In some circumstances—like when drupal_execute submits the form—the value will not be there. Use $form_state: it'll always contain the value, and it's the Drupal Way.
Mark Trapp
thanks a lot for the comment. I will start using that.
charan

related questions