Last active
August 29, 2015 13:57
-
-
Save ChezFre/9909038 to your computer and use it in GitHub Desktop.
Make a webform submit via Ajax instead of going to a seperate page Doesn't work with Webform 4.x, only with 3.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function hook_form_alter(&$form, &$form_state, $form_id) { | |
| if($form_id == "webform_client_form_22") { | |
| // 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' => 'hook_webform_js_submit', // update function | |
| 'wrapper' => 'webform-client-form-' . $nid, | |
| 'method' => 'replace', | |
| 'effect' => 'fade', | |
| ); | |
| } | |
| } | |
| function hook_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; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment