我在 Drupal 7 中有一个从自定义函数生成单选按钮的表单。在提交时,我获取所选按钮的索引值并通过另一个自定义函数对其进行处理。我的问题是,如果选择了第一个单选按钮,则不会生成索引 ($form_state['values']['stores'])。我想我可以将第一个按钮硬编码为相应数组中的第一个结果,但我真的很想弄清楚为什么会这样。
这是我的代码:
形式:
hook_form {
global $user;
$user_fields = user_load($user->uid);
$zipcode = $user_fields->zip_code['und']['0']['value'];
$defaults = !empty($form_state['values']['zip_code']) ? $form_state['values']['zip_code'] : $zipcode;
$storename = getmystorename($defaults);
$form['zip_code'] = array(
'#title' => t('Zip Code'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $defaults,
'#ajax' => array(
// #ajax has two required keys: callback and wrapper.
// 'callback' is a function that will be called when this element changes.
'callback' => 'settings_form_callback',
'wrapper' => 'textfields',
),
);
if(count($storename) > 0) {
$form['textfields'] = array(
'#prefix' => '<div id="textfields">',
'#suffix' => '</div>',
'#type' => 'fieldset' );
$form['textfields']['stores'] = array(
'#type' => 'radios',
'#title' => t('Choose your store:'),
'#options' => $storename,
'#default_value' => $storename[0], );
} else {
$form['textfields']['incorrect'] = array(
'#title' => t('Sorry, there are no stores available near you.'),
'#type' => 'fieldset', );
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
提交:
hook_form_submit {
$zipcode = $form_state['values']['zip_code'];
//Check that results were given for the zip code
if(!empty($form_state['values']['stores'])) {
$linkposition = $form_state['values']['stores']; }
//If results were given grab the store link
//if(isset($linkposition)) {
//querypath the array of the store name to grab link
$mystorelinks = getmystorelink($zipcode);
$storelink = $mystorelinks[$linkposition];
}
生成的数组:
Array ( [0] => Store1 [1] => Store2 [2] => Store3 [3] => Store4 [4] => Store5 [5] => Store6 [6] => Store7 [7] => Store8 [8] => Store9 [9] => Store10 )
Array ( [0] => http://www.example.com/stores/store1.html [1] => http://www.example.com/stores/store2.html [2] => http://www.example.com/stores/store3.html [3] => http://www.example.com/stores/store4.html [4] => http://www.example.com/stores/store5.html [5] => http://www.example.com/stores/store6.html [6] => http://www.example.com/stores/store7.html [7] => http://www.example.com/stores/store8.html [8] => http://www.example.com/stores/store9.html [9] => http://www.example.com/stores/store10.html )