11

I am a self-proclaimed newbie to creating forms in Drupal. I have a form hosted on a Drupal 7 website (using the webform module) and need to submit the form values to an external url. I've been researching this for awhile now and wrote a custom module that uses the webform module to submit externally using hook_form_alter and custom submit handler/function (code pasted below).

I've been using the following pages as guides, but I haven't been able to get the form to work: https://drupal.org/node/1357136 Using drupal_http_post() to submit to external site: What am I doing wrong?

Can someone let me know if I'm on the right track? Any guidance would be helpful!

<?php
function webform_extra_form_alter(&$form, &$form_state, $form_id)                 
{
   //only want form with nid 1171 to submit externally 
   //Note that "webform_client_form_1171" means modify the Webform form for the node with NID "1171". Adjust to match whichever webform node's form you're modifying
   if($form_id == 'webform_client_form_1171') 
       {
            $form['#action'] = url('https://[url path to external site]');
            $form['#attributes'] = array('enctype' => "application/x-www-form-urlencoded");
            $form['#submit'][] = 'webform_extra_submit';    
       }
}

// Adds a submit handler/function for the app signup form (Webform ID #1171) 

function webform_extra_submit($form, &$form_state) 
{
     // Changes can be made to the Webform node settings by modifying this variable
    //$form['#node']->webform;

    // Insert values into other database table using same input IDs as external db
    $option['query'] = array(
        $firstName => $form_state['values']['firstName'],
        $lastName => $form_state['values']['lastName'],
        $email => $form_state['values']['email'],
        $name => $form_state['values']['name'],
        $phone => $form_state['values']['phone'],
    );
    $url = url('https://[url path to external site]', $option); 
    $form_state['redirect'] = $url;
   //$form['#action'] = url('https:[url path to external site]');
   //$url = 'https://[url path to external site]';
   //$headers = array('Content-Type' => 'application/x-www-form-urlencoded',);
   //$response = drupal_http_request($url, $headers, 'POST', http_build_query($form_state['values'], '', '&'));
}
?>

2 Answers 2

17

In Drupal forms, form_alter hooks can be used to change almost anything in the form. Additional submit handlers can be handled, validation can be done, elements can be added, etc.

But for all of these to work, Drupal need to be the responsible party in both form build phase and the form submit phase.

When you set $form['#action'] = url('https://[url path to external site]');, you are actually removing Drupal from that latter responsibility.

Check the altered form - you will see that the form tag's action is set to the external site. When the form is submitted, browser sends all its data to that external site, and Drupal no longer can validate or perform submit functions in the form. I think this is the misconception happens.

If you don't want Drupal to validate the form, record the webform submission or do anything after the form submission and let the remote site do everything for that submission, your code will work just fine. You can remove $form['#submit'][] = 'webform_extra_submit'; part and the webform_extra_submit function itself.

However, if you do want to record the submission and submit the data to that remote site as well, you can do it like this:

function webform_extra_form_alter(&$form, &$form_state, $form_id)                 
{
   //only want form with nid 1171 to submit externally 
   //Note that "webform_client_form_1171" means modify the Webform form for the node with NID "1171". Adjust to match whichever webform node's form you're modifying
   if($form_id == 'webform_client_form_1171') 
       {
            $form['#submit'][] = 'webform_extra_submit';    
       }
}

// Adds a submit handler/function for the app signup form (Webform ID #1171) 

function webform_extra_submit($form, &$form_state) {

    $options = array();
    // Array keys are matching the key that the remote site accepts. URL encoding will be taken care later.
    $options['data'] = array(
        'firstName' => $form_state['values']['firstName'],
        'lastName' => $form_state['values']['lastName'],
        'email' => $form_state['values']['email'],
        'name' => $form_state['values']['name'],
        'phone' => $form_state['values']['phone'],
    );
    $options['data'] = http_build_query($options['data']);
    $options['method'] => 'POST';
    $url = 'https://[url path to external site]'; 

    // Put your additional headers here. Cookie can be set as well. 
    $headers = array('Content-Type' => 'application/x-www-form-urlencoded');

    $options['headers'] => $headers;

    // Submits data to the remote server from the drupal server. User will remain in the Drupal form submission chain.
    $response = drupal_http_request($url, $options);

}
3
  • THANK YOU for taking the time to clarify!! Extremely helpful and I really appreciate it.
    – ForTheWin
    Commented Nov 20, 2013 at 22:24
  • +1, but what if i have calculations in Drupal and post again to remote?
    – niksmac
    Commented Oct 17, 2015 at 10:21
  • After the last line executes, will the user be sent to the site mentioned in the $url ?
    – neelmeg
    Commented Apr 19, 2016 at 20:55
4

I have been trying to find a way to tackle this problem and I finally found the Webform Remote Post module

Webform Remote Post is a module that works along the Webform module. It eases the integration between Webforms and other web applications (including systems like Salesforce and Eloqua).

Hope it saves someone hours of looking!

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