What I'm trying to achieve
I am successfully using the Form API in Drupal 7 to dynamically alter Webforms to redirect to an off-site URL on submit.
What I need is twofold; I need (a) for the client to visit the offsite URL so the site can store a lead via query string parameters, as well as (b) for the same information to be documented in my local Drupal database.
Every lead must be documented, so I have tentatively ruled out JavaScript/AJAX as an option.
How I tried it
I thought the best way to do this was to set up a module with $form_state['redirect']
:
/**
* Implement hook_form_alter to change the form's submit action
*/
function myform_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['submit']['#submit'][]='myform_submit';
}
/**
* Callback as specified above
*/
function myform_submit($form, &$form_state) {
// Values for the query string
$vals = array('mykey' => 'myval');
// Redirect appropriately
$form_state['redirect'] = array(
// $path
'http://www.anotherwebsite.com/',
// $options
array('query' => $vals),
// $http_response_code
302,
);
}
I did that, and as expected, the user is redirected immediately after form submission. However, the Form API does not submit to my database before redirecting the user.
I read the FAPI documentation for Drupal 7. I sought using #action
, although it apparently reports only accepting internal values and my users must visit an offsite URL. I wound up using redirect
in the meantime, which was no longer documented but was suggested in the comments.
Where do I go from here?
EDIT: tenken's response works, but I have another problem!
My callback now uses array_unshift($form['#submit'],'my_callback')
and via drupal_http_request
, is submitting to both websites. However, my webform is still not saving submissions locally, and instead on submit, redirects to self (even with no hook_form_alter
).