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.

Thanks to this post on sending data using drupal_http_request (read the replies too!) I was able to write a submit function that looked something like this (obviously for the form with ID of my_module_newsletter_form):

<?php
function my_module_newsletter_form_submit($form, &$form_state) {
 
// remote URL we want to post to
 
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
 
// set our headers
 
$headers = array(
   
'Content-Type' => 'application/x-www-form-urlencoded',
  );
 
// the actual sending of the data
 
$response = drupal_http_request($url, $headers, 'POST', http_build_query($form_state['values'], '', '&'));

 
// 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.

Regarding redirection along with data submission

Hi GREG.HARVEY,

I have question on whether we can redirect and submit the data to the external website both together using drupal_http_request().

My Problem is I have a form where I need to validate the fields before submitting the form.

Once the fields are correct I want to submit the form values using POST method to a payment gateway.

Thank You ...

Regards,
Pavani

Lucky for me there are some

Lucky for me there are some Drupal gurus like you out there. I also bumped into the validation error and had no idea how to go around ti. Thanks for the tips. I really hope they'll work in my case as well.
______________
Mathew Farney

Oh, that's what I looked for.

Oh, that's what I looked for. But what if I need to redirect myself to the page, for example:
$url = 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
with ALL parameters that we are sending, at the same time?
I mean, it needs the POST request with build-in redirection.
That is how simple html form works.
Please, let me know what you think about it.

I'll try it because I hope it

I'll try it because I hope it would work. Nothing works other than that though.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.