Drupal 6: Programmatically submit a webform using drupal_execute()
In this snippet, I’ll show you how you can submit a webform programmatically using drupal_execute(). The first thing you’ll need to do is figure out what the $form_state data looks like when the webform is submitted, so you can recreate that structure and pass it into drupal_execute().
One way to accomplish this is to add a validation/submit handler to the form using hook_form_alter() and then output the contents of the submitted data (using krumo, print_r, etc).
The following code will prepend a validation handler to the webform $form, so we can dump the submitted data to the screen:
And here is the validation handler which will dump the submitted data to the screen. NOTE: krumo() is available from the devel module; you could use print_r() as well:
Now if I populate the webform with some data:
And submit the form, I will get the following debug output:
You’ll need to mirror the structure of the submitted data when creating your $form_state variable, which will be passed into drupal_execute().
In the following function, I show how you can submit a webform programmatically. There is even additional code in there which loads a user’s previously submitted data, if you care.