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.
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.
if()
thatdrupal_set_message()
's or otherwise logs what$form_id
is, etc. (And look into the less expensive strpos() vs strstr() when all done :)drupal_set_message('I am about to modify ' . $form_id);
inside yourif...
, it might shed light into why thatif
is succeeding on forms your don't expect it to. If you aren't seeing the above message, you could change it towatchdog('YOURMODULE','I am about to modify ' . $form_id);
and then, provided you have dblog()ing turned on, see it in your reports.alterwebform_webform_js_submit()
. Please see the question once again because the cause can be callback function too.