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.

  • 1
    D6 and D7 have very different Ajax frameworks. – Jeremy French Jul 4 '11 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. – E. de Saint Chamas Jul 6 '11 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 '12 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. – Danny Englander Feb 23 '12 at 16:20
8

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;
      }
    }
  • 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. – John Slegers Dec 12 '14 at 10:37
  • It goes into a custom module within your theme. – neelmeg Apr 19 '16 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.

  • 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 '12 at 17:04
-1

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

  • 1
    He's asking for Drupal 7, that module is not yet available for D7. – Danny Englander Feb 21 '12 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 '12 at 15:02

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.