2011-10-11 22 views
1

我目前正在使用Drupal 6中的自定義表單模塊。在此表單中,我使用了一個帶有大約10個不同選項的複選框字段。我似乎遇到的問題是,我從複選框中獲得的唯一輸出是「數組」。我花了幾個小時像一個瘋子一樣Google搜索,並且發現了很多關於如何創建複選框的教程,但沒有一個真正涉及到數據輸入後如何處理。Drupal 6從複選框數組中提取數據

這裏是複選框代碼:

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

感謝無論您提供什麼樣的幫助。

回答

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 
+0

非常感謝,很有效 – DanTheMan