2

The site I am working on has multiple Contact us type forms that are using a web-form plugin. They have a contact form that is on every page, 1 to 2 times per page. This contact form saves to the back-end and database when submitted.

Below is the basic form without the form elements.

<form action="/" accept-charset="UTF-8" method="post" id="webform-client-form-9" class="webform-client-form" enctype="multipart/form-data" _lpchecked="1">
  <input type="hidden" name="Provider" value="YourLeadSource">
  <input type="hidden" name="Client" value="L360">
  Fname: <input type="text"  name="fname" value="John"><br />
</form>

Now I have been tasked with keeping the current functionality, posting and saving to the database, but also adding new functionality to make it also POST or GET the same form submission to a third party site called leads360.

I need to have it also send a POST or GET request with the form data to https://secure.leads360.com/Import.aspx?Provider=TeledirectBusinessName&Client=CompanyName&CampaignId=12345.

In theory this should be really easy, if I can hook into the system and send a POST or even a GET request to that URL which will then post data into the third-party system.

The problem is my limited knowledge of Drupal. I know its plugin/hook system is nothing like WordPress or Magento.

Would you use some sort of hook in the backend, or intercept the post with JavaScript the way the Stripe API does for payments?

2

There's the Webform Remote Post module that does exactly this.

enter image description here

0
1

Yes you'd use a hook in the backend, in a custom module. Just to confuse things a little bit more, the type of hook you need to implement (an 'alter' hook) can also be implemented in a custom theme. But I'd advise sticking with the custom module for this, as the functionality you need is beyond what a theme should really be responsible for.

For the low down on Drupal hooks in general, see What is the basic concept behind hooks?.

In your specific case hook_form_alter() will do the trick; as the name suggests it's used to alter a form. There's even a more specific version, hook_form_FORM_ID_alter(), which lets you alter a form with a specific ID.

To run custom code on form submission, you need to add a submit handler to the form like in the following example:

function MYMODULE_form_webform_client_form_9_alter(&$form, &$form_state, $form_id) {
  $form['#submit'][] = 'MYMODULE_send_3rd_party_data';
}

The handler is just the name of a standard PHP function that you'll write, by convention prefixed with the name of your module. When the form is submitted, the function will run. In that function, you'll need to inspect the $form_state['values]` array, which contains the user-submitted values from the form.

function MYMODULE_send_3rd_party_data($form, &$form_state) {
  // Examples...
  $name = $form_state['values']['name'];
  $age = $form_state['values']['age'];
}

From there it's just a matter of submitting the data to the 3rd party service with the method of your choice. cURL is an obvious option, but Drupal also provides the drupal_http_request() function if you'd prefer to stay within the API.

4
  • Thanks for the detailed posts. The issue I am running into is I cannot find a custom module, it seems the web_form is just part of a page if that makes sense?
    – JasonDavis
    Aug 11 '13 at 19:55
  • @jasondavis You need to create a custom module, if you are not already using one for customizing the site.
    – apaderno
    Aug 12 '13 at 0:36
  • @Clive Thanks for your help. I have finally gotten a cuistom module and your example allows me to intersect the POST being made! So my end goal is to Pass the POSTed data off to a 3rd party API but I would also like to keep the existing saving to database functionality. So a quick question if you don't mind. Does this submit handler prevent my posted data from being posted to the Drupal database. Meaning am I over-riding that functionality or is my new handler just an additional action that will happen on that data?...
    – JasonDavis
    Aug 14 '13 at 3:54
  • ... If I am replacing that existing save to DB functionality. Could you possibly point me in the direction on how I can restore that part and also send the data to a 3rd party API? SO it will be 1 post but be saved to 2 locations? I am hoping that will be the default functionality but I am unable to test at the moment. Appreciate your Drupal knowledge
    – JasonDavis
    Aug 14 '13 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.