Closed (fixed)
Project:
Webform
Version:
7.x-3.11
Component:
Miscellaneous
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
6 Jul 2011 at 23:47 UTC
Updated:
19 Feb 2013 at 22:05 UTC
With Drupal 6 I used the Ajax module to integrate with webforms, so submission was handled in an ajax way. With D7 the Ajax module is no longer available. What is the alternative? Should I be using Ctools for such a thing, or Drupals default ajax mechanism? Any help or pointers greatly appreciated.
Comments
Comment #1
quicksketchI don't know, as I've never used AJAX Submit to begin with. Perhaps you should ask for alternatives from that module's queue? This doesn't really seem webform-specific.
Comment #2
davidd07 commentedIts not really Ajax module specific though, surely someone must of implemented a webform with an ajax submit..
Comment #3
lukusI'm trying to work out the same .. just posted an issue at http://drupal.org/node/1211712.
Comment #4
davidd07 commentedGlad someone else is looking for the same thing as well.. I have literally looked everywhere.. I used to achieve an Ajax submit with the Ajax module. But thats only on D6.
Comment #5
quicksketchLet's merge with #1211712: Adding Ajax to Webforms in Drupal 7 - Ajax Submit. No point in having two discussions on the same thing. I've marked that other issue as duplicate.
Comment #6
lukusOkay, I've got my ajax callback function working - whatever I return is added my form.
HOWEVER .. the replacement occurs whether the form validates or not.
Do the standard callbacks (via ['#submit'][] and ['#validate'][]) need to be looked after independently if ajax is used?
Comment #7
lukusI think it is webform specific, as there's no module for ajax submit. It's part of core and the standard FAPI in D7.
Comment #8
lukusI've discovered that
can be used within an ajax callback function to provide an indication of whether validation was successful. Because only part of the markup is being updated, the standard method for indicating validation errors - drupal_set_message() - can't be employed. Also, the highlighting of problem elements (via CSS class additions) isn't possible unless the form is refreshed.
Comment #9
lukusHi again
I've found that it's possible to alter a webform to use ajax on submit using HOOK_form_alter().
For example:
This will call the function THEMENAME_ajax_callback and will pass the ($form, $form_state) arrays to that function. The 'wrapper' attribute defines which blocklevel element will be replaced.
The callback function can return HTML, or a form element.
My problem is, if an incorrect value is specified the user needs to be alerted. To deal with this case I test form_get_errors() which seems to partly achieve what I'm hoping for. However the form itself isn't updated, so the classes that are used to specify elements that need attention aren't applied. The default error handing via drupal_set_messages() isn't carried out either.
I found that if I return the full form array directly from the callback function, the required messages are output in the required location, but after a second submission the newly added #ajax info added to the form via HOOK_form_alter isn't held persistently.
Looking into the code, it seems that when ajax_form_callback() is run, the form is rebuilt .. perhaps the modifications made via HOOK_form_alter() aren't being reapplied? If this is the case, I'm unsure where else I could apply the #ajax info into the form to deal with this.
Any advice would be appreciated.
Comment #10
bxCreative commentedThank you for posting the above HOOK_form_alter code - works great for me. In my ajax callback function, I am using:
Even when the form is submitted multiple times (due to validation errors), the '#ajax' form alter stays intact. My setup:
Does that help?
Comment #11
lukusThanks for the reply - I'll try out what you've suggested and see if it works for me as well.
Cheers
Luke
Comment #12
honigferd commentedHm. So how would the actual implementation look?
Would you need to create a custom module?
Comment #13
lukusThe code can sit in template.php in your theme if you're using D7. Previous versions of Drupal wouldn't allow HOOK_form_alter to exist within a theme, so a separate module would have been required.
I'll have a think about this, and work out if it could be adapted to work as a module. If I can think of a good solution, I'll post back the results here.
Comment #14
arbel commentedHello,
I've tried the above code, and the form submission isn't saved.
the ajax works, well, form validation works, and I get the thank you at the end, but nothing is saved at the end of the proccess.
works without the ajax.
here's my code
[code]
function enjoon_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'webform_client_form_32'){
$form['actions']['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
'callback' => 'enjoon_ajax_callback',
'wrapper' => str_replace('_','-',$form['#form_id']),
'effect' => 'fade',
),
'#value' => t('Submit'),
);
}
}
function enjoon_ajax_callback($form, $form_state) {
if(form_get_errors()){
return $form;
}
else{
return 'Thank you!';
}
}
[/code]
Comment #15
rasumo commentedThis approach preserves the confirmation message settings and may save someone else a lot of time:
Comment #16
ardnet commentedHi sumosystems, this probably sounds like a newbie question, can u provide the complete code please? especially with the javascript code, since we're talking about submitting webform using ajax here.
I also encounter this kind of issue, and had been pulling my hair since few days ago :P
Just FYI, my first thought of this is by using drupal_execute.
And I found this tutorial: http://thedrupalblog.com/programmatically-submit-webform-using-drupal-ex...
which I think can be done in Drupal 7, but apparently is not as easy as I thought.
I hope I'm talking the same problem here.
Thanks
UPDATED:
Sorry for my stupidness, it doesn't required any js file actually http://drupal.org/node/752056 :P
Anyway, I've just tried what sumosystem suggest there, but I got this when I checked in Firebug:
"NetworkError: 500 Internal Server Error - http://www.somedomain.com/system/ajax"
Anyone know why this happen?
Thanks
Comment #17
nlambert commentedHi,
I was wondering if anyone has had success in resetting the form?
After a successful submission, the form is not cleared. The user happens to find him/herself in front of an updatable form submission (by submission ID) : If you click submit again, you will get a "Submission Updated" message. You can even change the field values (which is actually kinda handy).
My question is, how could we reset the form and "prepare it" for a NEW submission after it has been successfully transmitted?
Hope that was clear.
Cheers
ADDITIONAL INFO :
For anyone looking, if you want ajax.js to handle the confirmation message you must :
1. Set a confirmation message under the form settings (node/%/webform/configure)
2. Choose "No redirect (reload current page)"
Comment #18
nlambert commentedHope this helps someone.
Let's take rasumo's example (#15)
The following is great if you want to reset the form. After the form has been successfully transmitted, the process serves up a fresh new form ready for transmission. HOWEVER, the fields are still populated. At this point, one could simple use jQuery('#form-id').clearForm();
On the contrary, by using the following you can allow the user to update the information that was just posted. They will get the "Submission Updated" status message each time they re-click the submit button.
Here's a great link for additional information http://envisioninteractive.com/drupal/add-ajax-to-a-webform-in-drupal-7/
Cheers
Comment #19
quicksketchClosing after lack of activity.
Comment #20
alex.skrypnykI've created a module webform_ajax_submit with an option per each webform node to be submitted via ajax.
The idea was taken from #15 (thanks rasumo).
http://drupal.org/sandbox/alex.designworks/1446550
Comment #21
gianfrasoft commented@rasumo @alex.designworks
module at #20 seems to work greatly.
#20 is the solution for me.
May thanks.
Comment #22
2pha#20 doesn't seem to work well when the form is in a block.
it works the first time it is submitted, but if it does not validate, the errors are shown as they should be but the form is no longer ajaxified.
Comment #23
gianfrasoft commented@2pha
It doesn't happen to me. Messages appear inside the block.
Comment #24
2pha@gianfrasoft
Even if it doesn't validate? The form can be used multiple times?
Comment #25
2phaI just tried it on a new install of drupal with only webform module.
This seems to only happen when the webform is in a block.
When the page is loaded, the form in the block is how it should be and has the 'ajax-processed' classes.
If you try to submit the form for the first time and the form is invalid, ajax is used and errors are shown above the form, as expected.
But,
the next time you try to submit, after and invalid try, the form no longer uses ajax and redirects to the page.
Am I the only one with this problem?
Comment #26
2phathe webform versions I have tried with are 7.x-3.15 and 7.x-3.16
problem exists in both...well, for me anyway.
Comment #27
gianfrasoft commentedMy installation still has no problem.
I'm using webform 7.x - 3.15.
Comment #28
2phaI have just tried it at home (as opposed to work) and it works fine. Hmmm, will have to wait till Monday to test the work site again.
Comment #29
2phaI have just found the problem.
It is because on the page with the webform block that I am trying to ajaxify, I also have the search block which is redered before the webform block.
When the page is first loaded, the ajax webform submit button id is 'edit-submit--2' (because the search block submit has the id of 'edit-submit'), but after the webform is submitted and returned with ajax, the webform submit button id is 'edit-submit'.
Removing the search block, makes the webform submit button have the id of 'edit-submit' on the first load aswell as when it is returned by ajax.....and all works as it should.
Now to find a work around.
Comment #30
2phaadded this to the form_alter in the ajax submit module to give the submit button a unique name.
This should probably be changed the webform module at some point.
$form['actions']['submit']['#id'] = 'edit-submit-'.$form['#form_id'];
Comment #31
gianfrasoft commentedInteresting, @2pha. I'll do the same.
Comment #32
David Stosik commentedHello,
I just rewrote Webform AJAX for Drupal 7, maybe you could have a look to my sandbox module?
http://drupal.org/sandbox/davidstosik/1468746
It's pending approval to be commited on webform_ajax repository.
Previous and next buttons, and also submit buttons are AJAXified, and don't need a page reload.
It also works in a block and handles submit confirmation and validation gracefully.
(You can choose for each webform to have an AJAX behaviour or not).
(As it is very small code, I'm even wondering if that could become part of Webform module...)
Throws a PHP Notice with Webform-3.16, fixed on 3.x-dev.
Regards,
David
Comment #33
mareks commentedThanks 2pha on #29.
Indeed. My search block was also confusing the alter code.
Thumbs up for the good tip! (y)
Comment #34
theodorosploumis#30 is absolute right. Thank you 2pha!
But except from
<input id="edit-submit-[form_id]">you must also alter the input parent
<div id="edit-actions-[form_id]">!So you finally must add these 2 lines in your hook_form_alter() function.
or using node id
Example:
Comment #35
David Stosik commentedHello,
Sorry if I sound rude, but I don't get why this issue is still active, although a release of Webform AJAX for Drupal 7 exists, and provides more features than what this sandbox module provides. You even are working on the same issues I had and already fixed before (see the issue queue).
Wouldn't "join forces" the right thing to do?
Regards,
David
Comment #36
alex.skrypnyk@David Stosik
I totally agree that "joining forces" is the right way to go.
Could you please release a stable version of your module, because only devs are available now and people are expecting to see stable?
Comment #37
langelhc commented@rasumo @alex.designworks
#20 is the solution for me, works!
In my case I was displaying a webform in a popup with Simple dialog module and when the user submit the webform still is in the popup and show the confirmation message, I tried with webform ajax module but don't show the confirmation message.
Thnks!