4

I use the advanced polls module in Drupal 7. I am trying to alter the behaviour so that the vote is submitted as soon as the radio button is selected (without having to hit Vote).

The module is using an ajax callback:

$form['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
  'callback' => 'advpoll_form_submit',
  'wrapper' => 'advpoll-form-' . $values->nid,
  'name' => 'submit1',
),
'#id' => 'edit-submit-advpoll-' . $values->nid,
'#value' => t('Vote'),

);

How can I invoke this callback with radio buttons?

I am happy for a quick and dirty javascript/jQuery solution as time is a real factor.

I have tried a jQuery solution:

$('input[type="radio"]').change(function() {
  $('#advpoll-form-296').trigger('click');
});

However this is not invoking the action as expected.

I cannot call submit() as I don't want to cause a page reload.

I have managed to get my original code to fire by adding a click event to the ajax array in the .module file.

advpoll.module:

function advpoll_choice_form($form, &$form_state, $values) {

$form['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
  'callback' => 'advpoll_form_submit',
  'wrapper' => 'advpoll-form-' . $values->nid,
  'name' => 'submit1',
  'event' => 'click', //Added click event
),
'#id' => 'edit-submit-advpoll-' . $values->nid,
'#value' => t('Vote'),

);

I am aware I should not override code within the module. How would I go about getting this into a theme hook?

3
  • 1
    have you tried using the jquery submit() function? api.jquery.com/submit
    – Jance
    Dec 14, 2015 at 16:51
  • Quick and dirty: $('#advpoll-form-296').submit();. The Drupal way is more complicated
    – Clive
    Dec 15, 2015 at 1:05
  • Cannot use submit() without reloading. Edited question to include more detail.
    – nickf
    Dec 18, 2015 at 12:17

2 Answers 2

2

This should do it:

$(function() {
  $('input[type="radio"]').on('change', function() {
    $(this).parents('form').submit();
  });
});

You might want to qualify the selector for $('input[type="radio"]') to the scope of the form you're working with, else if you include this in a JavaScript file that's accessed on many different pages, you'll definitely run into problems!

Demo: http://plnkr.co/edit/8448bjiywnVtbT3CV6s6?p=info

1
  • can't use submit() without causing a reload. Good suggestion on selector. Updated question with more detail. I just need to work out how to add the click event as a theme hook.
    – nickf
    Dec 18, 2015 at 12:35
1

It's been a while since this was posted but I hope this answer can help anyway

The simple way I found to do it (I'm using the advpoll module as well) is to add the same AJAX handler to the options dropdown using hook_form_alter

So, in my custom module:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  // The submit form has ID advpoll_choice_form
  if ($form_id == 'advpoll_choice_form') {
    foreach ($form as $key => &$value) {
      // It seems that the radio buttons are under an array labelled 'choice_X' 
      // where X is the number of options you have, so this just checks
      // it's doing it to the correct one
      if (substr($key, 0, 7) == 'choice_') {
        // Apply the same AJAX handler the submit button has to the radio buttons
        $value['#ajax'] = $form['submit']['#ajax'];
      }
    }
  }
}

Hope this helps someone in the future

Your Answer

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

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