Webform-将值从Drupal 7提交到外部URL


11

我是在Drupal中创建表单的自称新手。我在Drupal 7网站上托管了一个表单(使用webform模块),并且需要将表单值提交到外部url。我已经研究了一段时间,并编写了一个自定义模块,该模块使用webform模块使用hook_form_alter和自定义提交处理程序/函数(下面粘贴的代码)在外部进行提交。

我一直在使用以下页面作为指南,但无法使该表格正常工作:https : //drupal.org/node/1357136使用drupal_http_post()提交到外部站点:我在做什么错误?

有人可以让我知道我走的路是否正确吗?任何指导都会有所帮助!

<?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'], '', '&'));
}
?>

Answers:


15

在Drupal表单中,form_alter钩子可用于更改表单中的几乎所有内容。可以处理其他提交处理程序,可以完成验证,可以添加元素,等等。

但是,要使所有这些工作正常进行,Drupal需要在表单构建阶段和表单提交阶段都成为负责方。

设置时$form['#action'] = url('https://[url path to external site]');,您实际上是在从后一个职责中删除Drupal。

检查更改后的表单-您将看到form标记action已设置为外部站点。提交表单后,浏览器会将其所有数据发送到该外部站点,而Drupal不再可以验证或执行表单中的提交功能。我认为这是误解的发生。

如果您不希望Drupal验证表单,记录Web表单提交或在表单提交之后执行任何操作,并让远程站点为该提交做任何事情,您的代码将正常工作。您可以删除$form['#submit'][] = 'webform_extra_submit';部件和webform_extra_submit功能本身。

但是,如果您确实想要记录提交并将数据提交到该远程站点,则可以这样进行:

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);

}

谢谢您花时间来澄清!!非常有帮助,我非常感谢。
ForTheWin 2013年

+1,但是如果我在Drupal中进行计算并再次发布到远程该怎么办?
niksmac

最后一行执行之后,是否会将用户发送到$ url中提到的站点?
neelmeg

3

我一直在尝试找到解决此问题的方法,终于找到了Webform Remote Post模块。

Webform Remote Post是一个与Webform模块一起使用的模块。它简化了Webforms与其他Web应用程序(包括Salesforce和Eloqua之类的系统)之间的集成。

希望它可以节省一些人的时间!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.