【问题标题】:Drupal 7 programmatically submit data to webformDrupal 7 以编程方式将数据提交到网络表单
【发布时间】:2017-02-26 02:57:50
【问题描述】:

我在 Drupal 站点上有一个 webform,我需要使用以下代码以编程方式将数据发布到该站点:

// Get the node ID of the created Webform and load the node.
$node = node_load($nid);

global $user;
$uid  = $user->uid;

// Include files.
module_load_include('inc', 'webform', 'webform.module');  
module_load_include('inc', 'webform', 'includes/webform.submissions');

// Prepare the data for submission.
$data = array(
    2 => array('value' => array('question_id')),
    1 => array('value' => array('quiz_name')),
    3 => array('value' => array('your_feedback')),
);

$submission = (object) array(
    'nid' => $nid,
    'uid' => $user->uid,
    'submitted' => REQUEST_TIME,
    'remote_addr' => ip_address(),
    'is_draft' => FALSE,
    'data' => $data,
);

print_r($submission);

$sid = webform_submission_insert($node, $submission);

return "Submission {$sid} received!";

问题是提交已创建但它完全为空,即$data 数组未在提交中表示。

【问题讨论】:

标签: webforms drupal-7 submission programmatically-created


【解决方案1】:
global $user;
$nid = 4; //nid is the node id of your webform.

$node = node_load($nid); 

// The values to save. Take case about array indexes! (see below)
$data = array(
    '1' => array('0' => $type),
    '2' => array('0' => $method),
    '5' => array('0' => $volume),
    '6' => array('0' => $comment),
    '7' => array('0' => $phone),
    '8' => array('0' => $length)
);

$submission = (object) array(
    'nid' => $nid,
    'uid' => $user->uid,
    'submitted' => REQUEST_TIME,
    'remote_addr' => ip_address(),
    'is_draft' => FALSE,
    'data' => $data,
);

// Include the necessary files.
module_load_include('inc', 'webform', 'webform.module');
module_load_include('inc', 'webform', 'includes/webform.submissions');
webform_submission_insert($node, $submission); // Submit a submission.
webform_submission_send_mail($node, $submission); // Send webform's e-mails

$type$method$volume 等只是必须写入网络表单字段的变量。数组索引是 webform 的字段索引。

如何获取索引并找出哪个在哪里?

  1. 转到您的网络表单组件页面(例如 /node/4/webform where '4' 是一个网络表单 n​​id)。
  2. 将鼠标悬停在“编辑”链接字段上并查看链接源 (href)。它应该是这样的:http://[site-name]/node/4/webform/components/2?destination=node/4/webform,其中 '2' 是该字段的索引。
  3. 对所有必填字段执行相同操作。
  4. 如果出现问题,请查看 webform_submitted_data 数据库表。它有以下字段:
    • nid(网络表单 ID),
    • sid(提交ID),
    • cid(字段索引!),
    • 否(不知道,可能是多值字段的增量),
    • 数据(存储的字段数据)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2018-06-13
    相关资源
    最近更新 更多