我在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!";
问题是提交已创建,但它完全是空的i:e $data
数组未在提交中表示。
答案 0 :(得分:0)
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字段的变量。数组索引是Webform的字段索引。
如何获取索引并找出哪个在哪里?
/node/4/webform where
'4'是一个Webform nid)。http://[site-name]/node/4/webform/components/2?destination=node/4/webform
,其中“ 2 ”是该字段的索引。