tags:

views:

1594

answers:

2

I'm trying to create a custom form in Drupal 6 and everything seems to work okay with the code below including when submitted a new entry is created in 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.'));
}
?>
+1  A: 

Unless you specify it, $form_state['values'] will be a flat array and not a nested one so the values will be located at:

$form_state['values']['name']
$form_state['values']['description']

You could have debugged this problem yourself pretty easily using the devel module. With that 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 has defined, it creates a nice visual representation of the variable, where you click to show/hide the values inside arrays and class objects. Using that info you would have been able to fine where the values you needed was stored. It's a great tool in situations like this, where you want to inspect variables at runtime.

googletorp
Yep, great it works! Actually I'll file this under 'duh' because I can't believe I didn't try that in the first place! ;)
EddyR
A: 

Thanks, dpm was great advice for me as well! This type of problem has been irking me for awhile.

Equinger