Drupal 7 Drupal7自定义模块-通过Ajax发送表单内容,然后获取JSON响应

Drupal 7 Drupal7自定义模块-通过Ajax发送表单内容,然后获取JSON响应,drupal-7,drupal-modules,Drupal 7,Drupal Modules,我的自定义模块expertsqa.module中有以下代码 function expertsqa_menu() { $items = array(); $items['expertsqa/answerquestion'] = array( 'title' => 'Answer question', //page title 'description' => 'A form to mess around with.', 'page callback' => 'dr

我的自定义模块expertsqa.module中有以下代码

function expertsqa_menu() {
$items = array();

$items['expertsqa/answerquestion'] = array( 
  'title' => 'Answer question', //page title
  'description' => 'A form to mess around with.',
  'page callback' => 'drupal_get_form', 
  'page arguments' => array('expertsqa_answer_form'), //put the name of the form here
  'access callback' => TRUE
);

  return $items;
}

function expertsqa_answer_form($form, &$form_state) {

drupal_add_js(drupal_get_path('module', 'expertsqa') . '/js/jquery.form.js');

$suffix = '
 <script>
 jQuery(document).ready(function() {
    jQuery(\'#expertsqa-answer-form\').ajaxForm({
        target: "#output"
    });
 });
</script>
';

$form['price'] = array(
    '#type' => 'textarea', 
    '#title' => 'Type Answer',
    '#rows' => 5,
    '#columns' => 10,
    '#required' => TRUE, 
'#suffix' => $suffix,
);

$form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Click Here!'),
);

return $form;
}

function expertsqa_answer_form_submit($form, &$form_state) {
    drupal_json_output(array('foo', 'baa'));
    drupal_exit();
}
功能专家SQA_菜单(){
$items=array();
$items['expertsqa/answerquestion']=数组(
'title'=>'回答问题',//页面标题
“description'=>“要处理的表单。”,
“页面回调”=>“drupal获取表单”,
'page arguments'=>array('expertsqa\u answer\u form'),//在此处输入表单名称
“访问回调”=>TRUE
);
退回$items;
}
功能专家回答形式($form,&$form\u state){
drupal_add_js(drupal_get_path('module','expertsqa')。/js/jquery.form.js');
$suffix='1
jQuery(文档).ready(函数(){
jQuery(\'\\'expertsqa回答表单\').ajaxForm({
目标:“产出”
});
});
';
$form['price']=数组(
“#键入”=>“文本区域”,
“#title”=>“键入答案”,
“#行”=>5,
“#列”=>10,
“#必需”=>正确,
“#后缀”=>suffix美元,
);
$form['submit_button']=数组(
“#键入”=>“提交”,
“#value”=>t('单击此处!'),
);
返回$表格;
}
功能专家回答表格提交($form,&$form\u state){
drupal_json_输出(数组('foo','baa');
drupal_exit();
}

我希望通过ajaxform JQuery插件提交表单内容,该插件工作正常,然后希望由函数expertsqa\u answer\u form\u submit处理表单内容,该函数应返回JSON响应。请有人告诉我这样做的正确方法,因为它返回完整的html

您不需要包含任何JavaScript即可完成此操作。这是Drupal力量的一部分

这里有很多要解释的,而且以前已经做过了。我建议你读一下:

如果您阅读了这些文档,我相信您将能够完成您在这里打算做的事情

长话短说,就是在一个表单中,您有触发ajax调用的元素,然后在调用时重新加载元素。在此过程中,可以重新处理整个form_状态。一旦你掌握了窍门,你就会看到它极其强大

如果您将代码更新为正确的Drupal约定,但仍然无法使其正常工作,请在此处回复,我将更新我的答案以帮助您

祝你好运