我目前正在使用Drupal 6中的自定义表单模块。在此表单中,我使用了一个带有大约10个不同选项的复选框字段。我似乎遇到的问题是,我从复选框中获得的唯一输出是“数组”。我花了几个小时谷歌搜索疯狂的人,并找到了许多关于如何创建复选框的教程,但没有真正介绍一旦输入数据后如何处理数据。
这是复选框代码:
$form['message_box']['products'] = array(
'#type' => 'checkboxes',
'#title' => t('What services are you interested in ?'),
'#options' => array(
'home_and_auto' => t('Home & Auto Insurance'),
'auto' => t('Auto Insurance'),
'home' => t('Home Insurance'),
'other' => t('Other Personal Insurance'),
'business' => t('Business Insurance'),
'farm' => t('Farm Insurance'),
'life' => t('Life Insurance'),
'health' => t('Health Insurance'),
'rv' => t('Recreational Vehicle Insurance'),
'financial' => t('Financial Services'),
),
'#weight' => 39
);
我为数组
设置了一个变量$products = $form_state['values']['products'];
电子邮件正文的代码:
$body = 'New quote request from '.$sender.'<br><br>Email Address :'.$valid_email.'<br>'.'Phone No :'.$phone.'<br><br>'.'Address :<br>'.$street.'<br>'.$city.', '.$state.'<br>'.$zip.'<br><br>Interested in the following products<br>'.$products.'<br><br>'.$emessage;
感谢您提供的任何帮助。
答案 0 :(得分:1)
$opts = array(
'home_and_auto' => t('Home & Auto Insurance'),
'auto' => t('Auto Insurance'),
'home' => t('Home Insurance'),
'other' => t('Other Personal Insurance'),
'business' => t('Business Insurance'),
'farm' => t('Farm Insurance'),
'life' => t('Life Insurance'),
'health' => t('Health Insurance'),
'rv' => t('Recreational Vehicle Insurance'),
'financial' => t('Financial Services'),
);
$form['your_possibledynamyc_opts'] = array(
'#type' => 'value',
'#value' => $opts,
);
$form['message_box']['products'] = array(
'#type' => 'checkboxes',
'#title' => t('What services are you interested in ?'),
'#options' => $opts,
'#weight' => 39,
);
// in submit function
$products = array();
foreach ($form_state['values']['your_possibledynamyc_opts'] as $key => $val) {
if ($form_state['values']['products'][$key]) {
$products[] = $val;
}
}
$products = implode(', ', $products); // Here text of selected products by comma