views:

320

answers:

3

I created one module in drupal name as newmodule.I use form alter to alter user registration form for adding one field location.When i submit the form, how i can get the value of the new field which i created .

+1  A: 

The form value is stored in

$form_state['values']['field_name']

By default. If you set #tree to TRUE this behavior will change, and the values will be in a nested array instead of a flat one.

Two type of functions will be called, where you have access to the $form_state variable.

  1. Validation functions is used validate the form data, to check if the user inputted data is acceptable, like a valid email address etc. To add a validation function add this in your form alter implementation:

    $form['#validate'][] = 'name_of_your_validate_handler';
    
  2. Submit functions is used to act upon a form with valid data. Typically you insert data into the database, set redirects and such here, to add a submit function add this in your form alter implementation:

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

Both validation and submit functions take the same args:

function validate_or_submit_func(&$form, &$form_state) {
  // $form is the form array created by drupal_get_form
  // $form_state contains valuable info like, the submitted values and other options.
}
googletorp
A: 

New module also needs a _submit hook invoked so you can dump out the $form and $form_state values so you can see them.

Kevin
+1  A: 

To elaborate on the previous answers, somewhere in your hook_form_alter function, you want to tell the form to run your own #submit handler, e.g.:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['new_field'] = array( ... ); // You already did this, right?
  $form['#submit'][] = 'mymodule_submit_handler'; // Add this
}

Note: you should be appending on #submit here, not replacing it. Then in your submit handler, you can get the value easily, e.g.:

function mymodule_submit_handler($form, &$form_state) {
  $value = $form_state['values']['new_field'];
}
Scott Reynen