Drupal 6: form_state values ββempty on submit
I am trying to create a custom form in Drupal 6 and everything seems to work fine with the code below, including when a new record is posted to the database, however all the $ form_state values ββare empty. What am I missing?
<?php
function rate_form($form_state) {
$form = array();
$form['rate']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 30,
'#maxlength' => 100,
'#required' => TRUE,
);
$form['rate']['description'] = array(
'#type' => 'textarea',
'#title' => t('blah, blah'),
'#maxlength' => 1500,
);
$form['rate']['submit'] = array('#type' => 'submit', '#value' => t('Rate!'));
return $form;
}
print drupal_get_form($form_id);
function rate_form_submit($form_id, &$form_state) {
db_query("INSERT INTO {rate_comments} (name, description) VALUES ('%s', '%s')", $form_state['values']['rate']['name'], $form_state['values']['rate']['description']);
drupal_set_message(t('Thank you! Your rating has been added.'));
}
?>
source to share
If you don't specify it, it $form_state['values']
will be a flat array, not nested, so the values ββwill be located at:
$form_state['values']['name']
$form_state['values']['description']
You could debug this issue quite easily using the devel module. With this active you could do
function rate_form_submit($form_id, &$form_state) {
dpm($form_state);
//db_query("INSERT INTO {rate_comments} (name, description) VALUES ('%s', '%s')", $form_state['values']['rate']['name'], $form_state['values']['rate']['description']);
drupal_set_message(t('Thank you! Your rating has been added.'));
}
dpm is a function that devel defined, it creates a nice visual representation of a variable where you click to show / hide values ββinside arrays and class objects. Using this information, you would be able to estimate where the values ββyou want were stored. This is a great tool in situations like this where you want to check variables at runtime.
source to share
I had the same problem and found that in order to have a value as an array instead of a flat value, you use '#tree' => TRUE
on the parent form element.
http://drupal.org/node/751826
source to share