Drupal 6:form_state在提交时为空 [英] Drupal 6: form_state values empty on submit

查看:94
本文介绍了Drupal 6:form_state在提交时为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Drupal 6中创建一个自定义表单,并且所有似乎都可以使用下面的代码工作,包括在数据库中创建新条目时,所有$ form_state值都为空。我缺少什么?

 <?php 
函数rate_form($ form_state){
$ form = array();
$ form ['rate'] ['name'] = array(
'#type'=>'textfield',
'#title'=> t ),
'#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;
}

打印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('谢谢!您的评分已添加。
}
?>


解决方案

除非您指定,否则 $ form_state ['values'] 将是一个平面数组,而不是嵌套的,所以值将位于:

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

您可以使用devel模块很容易地调试此问题。与活性你可以做



 函数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('谢谢!您的评分已添加。
}



DPM是devel的定义了一个函数,它创建的一个很好的视觉表示该变量,您单击以显示/隐藏数组和类对象中的值。使用该信息,您将能够在存储所需值的位置。在这种情况下,这是一个很棒的工具,您希望在运行时检查变量。


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.'));
}
?>

解决方案

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.

这篇关于Drupal 6:form_state在提交时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆