Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have Ajaxified a webform using a (hook) custom module. The code used is:

 function alterwebform_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;

        $form['actions']['submit']['#ajax'] = array(
            'callback' => 'alterwebform_webform_js_submit',
            'wrapper' => 'webform-client-form-' . $nid,
            'method' => 'replace',
            'effect' => 'fade',
     );

  $form['#validate'] []='alterwebform_test';
      }
    }

The code in the callback function is:

function alterwebform_webform_js_submit(&$form, &$form_state, $form_id) {

  // define the $sid variable (submission id from webform)
    $sid = $form_state['values']['details']['sid'];

 // sid to know the form was properly submitted

    if ($sid) {

  //load up the webform node object

     $node = node_load($form_state['values']['details']['nid']);

  //create an array with the confirmation message of webform
        $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;
        }
    }

The web-form works fine . The problem is: The AJAX code above is affecting other forms (Comments form and User Register form).

How is it affecting ?

If other (comment and user-register) forms are submitted successfully in the first time then its fine. But if there is some error in the first time and we submit second time, then click on submit does not perform any action but show ajax effect.

The click on submit button now shows ajax kind of affect but is without any action.

enter image description here

As you can see in the picture, the ajax affect comes in this form. This happens if someone is clicking the button the second time (because of some error on first time).

What could be the possible reason for this and how should I fix this? Thank you in advance.

share|improve this question
3  
One way to track this down might be to put a debug statement inside your if() that drupal_set_message()'s or otherwise logs what $form_id is, etc. (And look into the less expensive strpos() vs strstr() when all done :) –  Jimajamma Dec 7 '12 at 14:37
    
@Jimajamma Could you please explain a little more. I really could not do the steps you said. Little more help please. Thank you :) –  Sandesh Yadav Dec 14 '12 at 9:26
    
certainly. if you put a drupal_set_message('I am about to modify ' . $form_id); inside your if..., it might shed light into why that if is succeeding on forms your don't expect it to. If you aren't seeing the above message, you could change it to watchdog('YOURMODULE','I am about to modify ' . $form_id); and then, provided you have dblog()ing turned on, see it in your reports. –  Jimajamma Dec 14 '12 at 15:58
    
I have tried printing $form_id using print and then die() function. It shows $form_id as 13 which is correct $form_id of the webform. I am updating the question by adding the code in the callback function alterwebform_webform_js_submit(). Please see the question once again because the cause can be callback function too. –  Sandesh Yadav Dec 15 '12 at 6:34
1  
It's unclear whether or not the above code is even being called, inappropriately, for the failed 'comment form and user-registration form' or the ajax form is caused from some other code. As jimajamma suggest, add in debug messages to detect if the above code is being called on the comment and registration forms. If the above code is being called on those forms, then you might consider (hook_form_BASE_FORM_ID_alter)[api.drupal.org/api/drupal/modules!system!system.api.php/… to narrow down which form the code should work on. –  jhr Dec 15 '12 at 10:15

1 Answer 1

up vote 1 down vote accepted
+50

The problem can be that ajax functionality is attached to button by its id attribute. Seems all three buttons - for webform, comment and user-register have the same id "edit-submit".

To fix this - just add unique id for webform submit button.

function alterwebform_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;

    $form['actions']['submit']['#ajax'] = array(
        'callback' => 'alterwebform_webform_js_submit',
        'wrapper' => 'webform-client-form-' . $nid,
        'method' => 'replace',
        'effect' => 'fade',

    );
    // Add unique id for submit button.
    $form['actions']['submit']['#id'] = 'webform-client-form-submit-' . $nid;

    $form['#validate'] []='alterwebform_test';
  }
}
share|improve this answer
    
Thanks a lot. Changing the id works. Awesome !! But by the time I got the answer, I have already created one custom form(not using the webform) :) . So now my confusion is which should I choose now. The form that I built from custom module or the already existing webform. Could you please tell me which would be better to use ?? Thanks a lot again !! –  Sandesh Yadav Dec 17 '12 at 12:00
    
If you want just send emails - use webform. If you create your own submit handler for specific actions - you can use your own form and don't use webform module at all. –  Eugene Fidelin Dec 17 '12 at 12:04
    
For now the need is only sending emails. So I will use webform . Thanks again :) –  Sandesh Yadav Dec 17 '12 at 12:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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