我无法从多步表单中获取值以插入数据库。我得到的只是每个字段的空值。
这是代码:
function multistep_profile_menu() {
$items['multistep_profile'] = array(
'title' => t('Multistep Profile'),
'page callback' => 'drupal_get_form',
'page arguments' => array('multistep_profile'),
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('access content'),
);
return $items;
}
function multistep_profile(&$form_state) {
// $form_state['storage']['step'] keeps track of what page we're on.
if (!isset($form_state['storage']['step'])) {
$form_state['storage']['step'] = 1;
}
//stores form data and carries it between steps
$default_value = '';
if (isset($form_state['storage']['values'][$form_state['storage']['step']])) {
$default_value = $form_state['storage']['values'][$form_state['storage']['step']];
}
switch ($form_state['storage']['step']) {
// Step 1 of Form
case 1:
$form['step_one'] = array(
'#title' => t('Step 1'),
'#type' => 'fieldset',
'#description' => t('Just getting started.'),
);
// Full Name
$form['step_one']['full_name'] = array(
'#title' => t('Full Name'),
'#type' => 'textfield',
'#description' => t('Please enter your name.'),
'#required' => TRUE,
'#default_value' => isset($default_value['full_name']) ? $default_value['full_name'] : '',
);
// College/University/Organization
$form['step_one']['college'] = array(
'#type' => 'textfield',
'#title' => t('College/University/Organization'),
'#default_value' => isset($default_value['college']) ? $default_value['college'] : '',
);
// Academic Disciplines
$form['step_one']['academic'] = array(
'#type' => 'textfield',
'#title' => t('Academic Disciplines'),
'#default_value' => isset($default_value['academic']) ? $default_value['academic'] : '',
);
break;
// Step 2 of Form
case 2:
$form['step_two'] = array(
'#title' => t('Step 2'),
'#type' => 'fieldset',
'#description' => t('Almost done.'),
);
// Job Title
$form['step_two']['job_title'] = array(
'#title' => t('Job Title'),
'#type' => 'textfield',
'#description' => t('Please enter your job title.'),
'#default_value' => isset($default_value['job_title']) ? $default_value['job_title'] : '',
);
// Department
$form['step_two']['department'] = array(
'#type' => 'textfield',
'#title' => t('Department'),
'#default_value' => isset($default_value['department']) ? $default_value['department'] : '',
);
break;
// Step 3 of Form
case 3:
$form['step_three'] = array(
'#title' => t('Step 3'),
'#type' => 'fieldset',
'#description' => t('Almost done, part 3.')
);
// Q1
$form['step_three']['q1'] = array(
'#title' => t('Why do you want to develop a globally networked course?'),
'#type' => 'textarea',
'#description' => t(''),
);
break;
//Confirmation
case 4:
$form['thanks'] = array(
'#value' => '<p>'. t('Your Profile is complete.') .'</p>',
);
break;
}
//programmatically display buttons
if ($form_state['storage']['step'] > 1) {
$form['previous'] = array(
'#type' => 'submit',
'#value' => t('<< Previous'),
);
}
if ($form_state['storage']['step'] != 4) {
$form['next'] = array(
'#type' => 'submit',
'#value' => t('Continue >>'),
);
}
else {
$form['finish'] = array(
'#type' => 'submit',
'#value' => t('Finish'),
);
}
return $form;
}
function multistep_profile_submit($form, &$form_state) {
//Save the values for the current step into the storage array.
$form_state['storage']['values'][$form_state['storage']['step']] = $form_state['values'];
//Check the button that was clicked and change the step.
if ($form_state['clicked_button']['#id'] == 'edit-previous') {
$form_state['storage']['step']--;
}
elseif ($form_state['clicked_button']['#id'] == 'edit-next') {
$form_state['storage']['step']++;
}
elseif ($form_state['clicked_button']['#id'] == 'edit-finish') {
//You should store the values from the form in the database here.
dpm($form_state);
// INSERT into DB
$full_name = $form_state['values']['full_name'];
$college = $form_state['values']['college'];
$academic = $form_state['values']['academic'];
$job_title = $form_state['values']['job_title'];
$department = $form_state['values']['department'];
$q1 = $form_state['values']['q1'];
db_query("INSERT INTO {multistep_profile} (full_name, college, academic, job_title, department, q1, created) VALUES
('%s', '%s', '%s', '%s', '%s', '%s', %d)", $full_name, $college, $academic, $job_title, $department, $q1, time());
drupal_set_message(t('Your profile has been saved.'));
unset($form_state['storage']);
//Go to this page after completing the form.
//$form_state['redirect'] = 'node';
}
}
使其进入数据库的唯一值是“创建”日期时间戳。
我运行了一个 DPM 来检查这些值是否在表单步骤之间传递,它们是,我只是不知道如何让它们插入到数据库中。有什么帮助吗?