hook_form_submit 没有被调用
我正在尝试提交表单并使用 hook_form_submit。
问题是表单是通过 ajax 显示的,这会导致 hook_form_submit 不被调用。
$items['ajaxgetform/%'] = array(
'page callback' => 'ajaxgetform',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
function ajaxgetform($form_id) {
drupal_get_form($form_id);
return drupal_json($panel);
}
function_myform_form($form_state) {
$form['myform'] = array(
'#title' => 'myform value',
'#type' => 'textfield',
'#default_value' => 'myform default value'
);
$form['#action'] = url('myurl');
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'submit'
);
$form['#ajaxsubmit'] = TRUE;
return $form;
}
hook_form_alter()
确实被调用。
下面没有被调用吗?
function myform_form_submit($form, $form_state) {
// ...
}
我不确定这是否是一个常见问题,但我已经被困了几个小时试图让它发挥作用。
如果我删除 $form['#action'] = url('myurl');
myform_form_submit()
会被调用。但是我得到一个带有杰森脚本的白屏。
I'm trying to submit a form and use hook_form_submit.
The problem is the form is displayed via ajax and this results in hook_form_submit not being called.
$items['ajaxgetform/%'] = array(
'page callback' => 'ajaxgetform',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
function ajaxgetform($form_id) {
drupal_get_form($form_id);
return drupal_json($panel);
}
function_myform_form($form_state) {
$form['myform'] = array(
'#title' => 'myform value',
'#type' => 'textfield',
'#default_value' => 'myform default value'
);
$form['#action'] = url('myurl');
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'submit'
);
$form['#ajaxsubmit'] = TRUE;
return $form;
}
hook_form_alter()
does get called.
Below doesn't get called?
function myform_form_submit($form, $form_state) {
// ...
}
I'm not sure if this is a common problem, but i've been stuck for hours trying to make it work.
If I remove $form['#action'] = url('myurl');
myform_form_submit()
gets called. However I get a white screen with jason script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有
hook_form_submit()
。相反,您可以使用$form['#submit']
注册提交处理程序。因此,如果您想在提交表单时调用myform_form_submit()
,请在
myform_form()
中 添加:查看 5.x 到 6.x 表单更改 和 表单 API 参考了解更多信息。There is no
hook_form_submit()
. Instead, you register submit handlers with$form['#submit']
. So, if you want to callmyform_form_submit()
when the form gets submitted, add:to
myform_form()
. Take a look at the 5.x to 6.x form changes and the Forms API reference for more info.您的表单是否显示在
myurl
页面上?为了处理表单提交,要显示表单(使用drupal_get_form()
) 在页面上用作操作。您也可以尝试使用表单
#redirect
到着陆页 URL,而不是 #action。这样,表单将提交到其生成的 URL,但用户在处理后会重定向到您的目标页面。Is your form displayed on the page at
myurl
? In order for a form submission to be processed, the form as to be displayed (usingdrupal_get_form()
) on the page used as action.You may also try to se the form
#redirect
to the landing page URL instead of its #action. This way, the form is submitted to its generating URL but the user is redirected to your destination page after processing.