2

I have a themed table on a form. To delete entries in this table I render buttons for each row having a special idx and an ajax callback for each button.

The buttons are rendered perfectly, but when I click on the button, the standard submit routine is called instead of the assigned callback routine. Is there anything I missed?

// build header for table
$pheader = array('Name','Action');
// build data array for table
$aPersonsTable = array();
foreach ($aPersons as $key => $value) {
  $form_button['button'.$key] = array(
    '#id' => 'del-p-'.$key,
    '#name' => 'del-p-'.$key,
    '#idx' => $key,
    '#type' => 'submit',
    '#value' => 'Delete',
    '#submit' => 'delete_auction_person_ajax_callback_submit',
    '#attributes' => array('class'=>array('ajax-processed')),
    '#ajax' => array(
      'callback' => 'delete_auction_person_ajax_callback',
      'progress' => array('type' => 'none'),
      'effect' => 'fade',
    )
  );
  $deleteButton = drupal_render($form_button['button'.$key]);
  $row = array($value['dname'],$deleteButton);
  $aPersonsTable[]=$row;
}

$form['persons']['ptable'] = array(
  '#theme' => 'table',
  '#header' => $pheader,
  '#rows' => isset($aPersonsTable)?$aPersonsTable:null,
  '#disabled' => TRUE,
  '#title' => 'Users currently assigned',
  '#empty' => t('No users assigned yet!'),
  '#prefix' => '<div id="ptable_wrapper">',
  '#suffix' => '</div>',
);
1

3 Answers 3

2

Remove the '#submit' => 'delete_auction_person_ajax_callback_submit', it does not seem necessary. Adding ajax-processed class on the button doesn't seem necessary either. The function delete_auction_person_ajax_callback receives $form and $form_state, and will tell you what input was clicked that triggered the callback, from there you can do whatever it is you need to do.

I built a lot of tables this way, but I don't think I had the form items as a submit type. If I recall, I used buttons.

1

You set #executes_submit_callback to false on your submit button to prevent that the submit callback is executed.

$form_button['button'.$key] = array(
    '#id' => 'del-p-'.$key,
    '#name' => 'del-p-'.$key,
    '#idx' => $key,
    '#type' => 'submit',
    '#value' => 'Delete',
    '#submit' => 'delete_auction_person_ajax_callback_submit',
    '#executes_submit_callback' => false,
    '#attributes' => array('class'=>array('ajax-processed')),
    '#ajax' => array(
      'callback' => 'delete_auction_person_ajax_callback',
      'progress' => array('type' => 'none'),
      'effect' => 'fade',
    )
  );
3
  • Thanks! It doesn't work better with that line. Clicking the button still submit's the whole document instead of doing an AJAX request.
    – schulle877
    Feb 10, 2014 at 12:52
  • Are your rows with the delete button generated after an ajax callback ? Feb 10, 2014 at 13:49
  • No. They are generated when loading the 'initial' page.
    – schulle877
    Feb 12, 2014 at 7:32
0

If you want to prevent submit, use #ajax['prevent'] attribute:

'#ajax' => array(
  'callback' => 'delete_auction_person_ajax_callback',
  'progress' => array('type' => 'none'),
  'effect' => 'fade',
  'prevent' => 'submit',
)
5
  • Thanks for that. I added the prevent line to my code but it has no effect. When I compare my rendered buttons to a working AJAX button there is a class missing in the html code of the rendered button: <input type="submit" id="del-p-8c19f571e251e61cb8dd3612f26d5ecf" name="del-p-8c19f571e251e61cb8dd3612f26d5ecf" value="Delete" class="form-submit"> There correct AJAX button looks like follows: <input type="submit" id="edit-add-person-btn" name="add-person-btn" value="Add person" class="form-submit ajax-processed"> That may be the problem. But how to solve it? Manually adding the class...
    – schulle877
    Feb 7, 2014 at 9:23
  • ... 'ajax-processed' doesn't work.
    – schulle877
    Feb 7, 2014 at 9:25
  • Does not work or change anything. Button still not loading via ajax.
    – kwoxer
    Mar 22, 2016 at 14:45
  • @kwoxer sorry to hear that, but I only pointed to a specific form api prameter. If it got faulty in the meantime, I can't do a thing about it. If your implementation is faulty, I can't know.
    – Mołot
    Mar 22, 2016 at 14:48
  • Yeah no problem, but someone else might try this here and it does not work. So I safe his time on trying it out :)
    – kwoxer
    Mar 22, 2016 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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