Drupal FAPI 表单调用回调两次
第一篇关于堆栈溢出的文章...所以对我宽容一些!
对于简单表单提交的 Drupal FAPI 多重回调问题,似乎没有合适的解决方案。
问题:我的表单在提交时会向相应的数据库表添加两个条目。鉴于只有一次调用将其添加到数据库,我认为可以安全地假设查询运行两次(因此是双条目)。
以下代码可能有助于为解决方案提供基础。哦,它也是 Drupal 7,所以文档仍然以 D6 为中心。
function mymodule_sidebar_form_add_submit(&$form, &$form_state) {
$form_values = $form_state['values'];
$se_title = check_plain(trim($form_values['title']));
$se_link = url(trim($form_values['link']));
$se_content = check_plain(trim($form_values['content']));
$se_image = isset($form_values['image']) ? $form_values['image'] : '';
// The multi-line part below is actually a single line the real code
$query = sprintf("INSERT INTO sidebar_element(title, image_url, content)
VALUES ('%s', '%s', '%s');", $se_title, $se_image, $se_content);
db_query($query);
drupal_set_message(t('Sidebar Element has been added successfully.'));
}
...我的表单函数包含一个提交按钮:
$form['submit'] = array(
'#value' => t('Add Sidebar'),
'#type' => 'submit',
'#title' => t('Add Sidebar'),
'#submit' => array('mymodule_sidebar_form_add_submit'),
);
我想我需要回答的问题是:
- 为什么首先会有双重回调?
- 有没有办法识别第一个回调?
预先感谢大家。
First post on stack overflow... so go easy on me!
There doesn't seem to be a suitable solution to the Drupal FAPI multiple callback issue for simple form submissions.
THE PROBLEM: My form, when submitted, adds two entries to the respective database table. Given that there is only one call to add it to the database, I feel it's safe to assume that the query is run twice (hence the dual entries).
The following code may help to provide a basis for a solution. Oh, it's Drupal 7 too, so documentation is still very much D6 centric.
function mymodule_sidebar_form_add_submit(&$form, &$form_state) {
$form_values = $form_state['values'];
$se_title = check_plain(trim($form_values['title']));
$se_link = url(trim($form_values['link']));
$se_content = check_plain(trim($form_values['content']));
$se_image = isset($form_values['image']) ? $form_values['image'] : '';
// The multi-line part below is actually a single line the real code
$query = sprintf("INSERT INTO sidebar_element(title, image_url, content)
VALUES ('%s', '%s', '%s');", $se_title, $se_image, $se_content);
db_query($query);
drupal_set_message(t('Sidebar Element has been added successfully.'));
}
... and my form function contains a submit button:
$form['submit'] = array(
'#value' => t('Add Sidebar'),
'#type' => 'submit',
'#title' => t('Add Sidebar'),
'#submit' => array('mymodule_sidebar_form_add_submit'),
);
I guess the questions I need answered are:
- Why is there a double callback in the first place?
- Is there a way to identify the first callback?
Thanks in advance to all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要找出第二个调用来自哪里,最简单的方法是安装 devel.module 并在提交回调中使用 ddebug_backtrace() 。您可能还需要禁用 HTTP 重定向才能看到它 (exit())。
但更重要的是,使用 API,Luke!
这就是您的插入查询应该是什么样子,您所做的事情是不安全的!
对于 SELECT,使用 db_query() 和命名占位符:
To find out where the second call is coming from, the easiest way is to install devel.module and use ddebug_backtrace() in your submit callback. You might need to disable the HTTP redirecto to see it, too (exit()).
But more importantly, use the API, Luke!
This is how your insert query should look like, what you are doing is insecure!
And for SELECT, use db_query() with named placeholders:
并替换
为
And replace
with
对于 Drupal 7
作为示例,请阅读 node_form () 代码
For Drupal 7
As example read node_form() code