8

I have tried to alter the webform rendering using hook_form_alter() and hook_node_view() in a custom module so I could add '#ajax' somewhere.

Has anybody already had some experience working with webform and ajax on D6 or D7 ? I guess the logic would be the same for D6 and D7 then just the implementation changes.

4
  • 1
    D6 and D7 have very different Ajax frameworks. Jul 4, 2011 at 9:43
  • Yes they do, but its not an issue. The real issue i'm having is that i can't find a hook to alter the Webform form and add an ajax event through Drupal form API. Jul 6, 2011 at 12:34
  • Do you have a specific question regarding Ajax? This sounds conversational. Also, just for reference: api.drupal.org/api/drupal/includes--ajax.inc/group/ajax/7
    – Citricguy
    Feb 2, 2012 at 7:27
  • I don't really like posting answers just with links to "how to" blog posts without any explanation of my own so I'll just add this as a comment: envisioninteractive.com/drupal/… -- if this ends up working, we can consider adding it as an answer perhaps. It seems like this is what you might need. Feb 23, 2012 at 16:20

3 Answers 3

9

The Ajax Module works for me in Drupal 6.

For Drupal 7:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
      // see if webform_client_form_ is in the form_id
      if(strstr($form_id, 'webform_client_form_')) {
        // get the nid so we can use it in the wrapper value
        $nid = $form['#node']->nid;
        // add the ajax properties to the submit button
        $form['actions']['submit']['#ajax'] = array(
          'callback' => 'mymodule_webform_js_submit',
          'wrapper' => 'webform-client-form-' . $nid,
          'method' => 'replace',
          'effect' => 'fade',
        );
      }
    }

function mymodule_webform_js_submit($form, $form_state) {
      // define the $sid variable (submission id from webform)
      $sid = $form_state['values']['details']['sid'];
      // if we have a sid then we know the form was properly submitted, otherwise, we'll just return the existing $form array
      if ($sid) {
        // first we have to load up the webform node object
        $node = node_load($form_state['values']['details']['nid']);
        // create an array up with the confirmation message, retreived from the webform node
        $confirmation = array(
          '#type' => 'markup',
          '#markup' => check_markup($node->webform['confirmation'], $node->webform['confirmation_format'], '', TRUE),
        );
        // return the confirmation message
        return $confirmation;
      }
      else {
        // return the form
        return $form;
      }
    }
2
  • What exactly is this code supposed to do? I added it to my theme (after changing the hook's names to match my theme), but I don't notice any impact. Dec 12, 2014 at 10:37
  • It goes into a custom module within your theme.
    – neelmeg
    Apr 19, 2016 at 18:02
0

One of the easiseast way if you are looking to ajaxify just a particular form will be to add the jquery form plugin. Its pretty straight forward.

Add the below code to you page preprocess function in template.php file.

  1. First add the jquery plugin with below code.

    drupal_add_js(drupal_get_path('theme', 'your_theme') . "/js/jquery.form.js");

  2. Then Add the below code, replace #your_form_ID with form id of yours

    drupal_add_js('

                (function($){ 
    $(document).ready(function() { 
    
                $("#your_form_ID").ajaxForm(function() { 
                    alert("Thank you for your comment!"); 
                }); 
    });     }(jQuery));;
    
           ', 'inline');
    

That's all you are done. Maybe you want to consider loading the scripts only the pages you need.

1
  • I have been considering it. Now I am thinking of making it an module with a config page so all can benefit. Does anything like that exist?
    – esafwan
    Jun 17, 2012 at 17:04
-1

See if the Ajax module can help you with that. See the documentation as well

2
  • 1
    He's asking for Drupal 7, that module is not yet available for D7. Feb 21, 2012 at 16:43
  • 1
    To be fair, although the question is tagged as 7, it does explicitly ask if anyone had experience with ajax in D6 or D7.
    – EmmyS
    May 8, 2012 at 15:02

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.