Validating & Submitting Forms To Other Websites With Drupal

So you've got a client who wants to have a Drupal form send something off to another website. In my case my client wanted to manage their newsletter from the SalesForce CRM (which they doubtless paid gazillions of dollars for, so they'd better bloody well use it, etc.) so newsletter sign-ups needed directing there.

No problem, I thought - build a Drupal form in the usual way and add the '#action' form attribute, setting it to the remote URL the form needs to be submitted to. Tried that, it worked. Happy days.

Now to add some validation... What the deuce?? My validation has no effect! Turns out this is by design. If you post your data off to another site, then that site has the task of validating. Except in my case it doesn't. Rats.

Now what? Well I found this post about using drupal_execute() for executing your form, but this is no good - it will never work, because drupal_execute() is for local use, assumes so and ignores '#action' in forms.

So it was longwave in #drupaluk IRC (again!) who suggested using drupal_http_request instead. This way I can leave my form alone, let it do the usual stuff in the usual Drupal way (present a form, validate the submission) and then post it off in the submit function for the form.

// if we get a good response, thank user for submission
if ($response->code == 200) {
drupal_set_message(t('Thank you for subscribing to our newsletter.'));
// otherwise let them know something went wrong
} else {
drupal_set_message(t('There was a problem with your submission. Please try again later.'), 'error');
}
}
?>

And that's all there is to it. Once you have let Drupal handle building the form and managing all the validation you can *then* submit your data off to another site. I have tested this and the response data looks good. Only thing left to work out is why I'm not getting my debug email, as I am getting a positive response from the SalesForce script, but that's another story. Wish me luck! =(

Edit: Got my debug email in the end. Turned out I needed to set the Content-Type request header properly. Updated the above code snip to include the necessary change.

We’re Code Enigma

We’re one of the most experienced Drupal teams in Europe, best known for our work on large, technically challenging projects for all kinds of clients.

Our team is passionate about Drupal and open source software. Our whole company spends at least four weeks per year working on Drupal modules or other open source projects. We’re also strongly committed to putting design first, taking a mobile-first, content-out approach to creating websites. This ensures that the sites we build combine the power of Drupal with best practice design and development.