3

I want to have buttons for remove selected field from form.

    $form['questions']['buttons'][$counter] = array(
        '#type' => 'button', 
        '#name' =>'button_'.$counter, 
        '#value' => t('Remove'),
        '#submit' => array('my_mod_remove_submit'),
    );

Submit function:

function my_mod_remove_submit($form, &$form_state) {
    drupal_set_message('<pre>'.print_r($form_state, 1).'</pre>');
}

But it seems that this function is never invoke.

4

The problem with your code is the default value for the form field attributes. In the case of the "button" form field, these are the default values:

  • #name => 'op'
  • #button_type => 'submit'
  • #executes_submit_callback => FALSE
  • #ahah['event'] => 'mousedown'

Those are also the default values for Drupal 7.

If you want to use a submission callback for a button, then you need to change the form field definition to the following one:

$form['questions']['buttons'][$counter] = array(
  '#type' => 'button', 
  '#name' =>'button_' . $counter, 
  '#value' => t('Remove'),
  '#executes_submit_callback' => TRUE,
  '#submit' => array('my_mod_remove_submit'),
);

As alternative, instead of using a "button" form field, you should use a "submit" form field.

1

Change the input type from "button" to "submit"

0

I think you want to manage multiple submit buttons associated with their data to perform operations on it.

The better approach is to use check boxes with a single submit button.

e.g.

function my_form(){
  $data_ids = array();

  foreach($data_set as $data){
    $data_ids[$data] = '';
    $form['data']['data_preview'][$data['id']] = array('#value' => $data['value']);
  }

  $form['selection'] = array(
    '#type' => 'checkboxes'
    '#options' => $data_ids;
  );

  $form['process'] = array(
    '#type' => 'submit',
    '#value' => 'do',
  );

  $form['#theme'] = 'my_form';
}

Theme handler (do not forget to implement my_form entry in hook_theme)

function theme_my_form($form = array()){
  $rows = array();
  foreach(element_children($form['selection']) as $id) {
    $fields = array();
    $fields[] = drupal_render($form['selection'][$id]);
    $fields[] = drupal_render($form['data']['data_preview'][$id]);
    $rows[] = $fields;
  }

  $output = theme('table', array('Select', 'Data'), $rows);
  $output .= drupal_render($form);// Render rest of form
  return $output;
}

Now form submit handler

function my_form_submit(&$form, $form_state){
  $clicked_button = $form_state['clicked_button']['#value'];
  $values = $form_state['values'];

  if ($clicked_button == $values['process']){
       //Here check $value['selection'] array and process accordingly
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.