Drupal 6: form_state values empty on submit

☆樱花仙子☆ 提交于 2020-01-02 17:16:16

问题


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:


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.




回答2:


I had the same problem and discovered that in order to have the value as an array instead of a flat value, you use '#tree' => TRUE, in the parent form element. http://drupal.org/node/751826




回答3:


This may help

implode(arg(),'/');


来源:https://stackoverflow.com/questions/1464056/drupal-6-form-state-values-empty-on-submit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!