3

I have made a form with form loop as I am setting the $form_state['number'] for the fields count.

I want to remove a full row after clicking on remove button in front of row by taking row_id. So I want to ask is there any way to pass id of row in submit function of remove button or check which button pressed and fetching id of that button in submit function.

 for ($i = 1; $i <= $form_state['number']; $i++) {
    $form['myform'][$i]['email'] = array(
        '#type' => 'textfield',
        '#size' => 18,
        '#autocomplete_path' => 'mypath',
        '#prefix' => '<div id="feedbackrow_'.$i.'">',
    );
    $form['myform'][$i]['roles'] = array(
        '#type' => 'select',    
        '#options' => array('' => '', 'master' => 'master', 'student' => 'student'),
    );
     $form['oldfeedbackmarkup'][$i]['remove'] = array(
       '#type' => 'submit',
       '#value' => t('Remove'),
       '#submit' => array('mysubmit_button'),
       '#ajax' => array(
              'wrapper' => 'myform_wrapper',
              'callback' => 'submit_by_ajax',   
        ),
        '#prefix' => '<div class="form-item">',
        '#suffix' => '</div></div>',
    );
}
0
1

It's actually easier than you think :)

You can access a $form_state['triggering_element'] in your ajax callback function.

Something like this:

function my_ajax_callback($form, $form_state) {
  $ps = &$form_state['triggering_element']['#parents'];
  $k = $ps[2]; // field collection row number 
  $collection = $form[$ps[0]][$ps[1]][$ps[2]];
  $element = $ps[3];
  ...

that was the snippet to access a changed textfield inside a field collection with multiple rows. Just adopt it for your particular form structure.

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.