帮助ajax回调和drupal_process_form [英] Help with ajax callback and drupal_process_form

查看:134
本文介绍了帮助ajax回调和drupal_process_form的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过nodeapi添加的窗体,仅在视图模式上显示。用户可以从选择菜单中选择一个项目,并且他们的选择将自动保存到数据库,同时使用hook_menu回调。如果用户已停用JavaScript,则会使用api格式正常提交。这一切工作正常,但现在为了安全原因,我想通过形式api提交ajax版本。我的form_name_submit很简单:

  function mymodule_test_form_submit($ form,& $ form_state){
global $ user;
db_query(INSERT INTO {mymodule}(nid,uid,status,created)VALUES(%d,%d,。time()。),$ form ['#parameters' [2],$ user-> uid,$ form_state ['worth'] ['mymodule_status']);
}

我的ajax:

  $('。mysubmit')。css('display','none'); 
$('。myclass')。change(function(){
$ .ajax({
type:'POST',
url:Drupal.settings.basePath + mymodule / set /'+ $ nid +'/'+ $('。myclass')。val(),
dataType:'json',
data:{'ajax':true,'form_build_id ':$('#mymodule-form input [name = form_build_id]')。val()}
});
});

我的回调函数:

  function mymodule_set($ nid,$ status){
$ form_build_id = $ _POST ['form_build_id'];
$ form_state = array('storage'=> NULL,'submitted'=> FALSE);
$ form = form_get_cache($ form_build_id,$ form_state);
$ args = $ form ['#parameters'];
$ form_id = array_shift($ args);
$ form ['#post'] = $ _POST;
$ form ['#redirect'] = FALSE;
$ form ['#programmed'] = FALSE;
$ form_state ['post'] = $ _POST;
drupal_process_form($ form_id,$ form,$ form_state);
}

最初我的回调函数与我的submit函数大致相同, '试图使用提交函数与ajax以及。这是正确的方式使用drupal_process_form?窗体从缓存中抓取,它获得验证并提交如果没有错误?我使用本教程中的一些代码来应用于我的情况: http://drupal.org/node/331941似乎没有任何我想要做的例子。我也有$ form ['#cache'] = TRUE;在我的表单函数。



drupal_process_form如何比较提交的值与原始表单检查完整性?我应该添加我的值到form_state,因为表单状态将是空的ajax。



感谢。

解决方案

我不得不做过类似于你在过去和阅读同一个教程,你发布,不幸的是没有多少信息avalaible这个,它是令人头痛,使它工作。我不太清楚细节,但我看看我写的代码,这里有几个建议,可能对你有用:





如果您以节点形式执行此操作,请添加#ahah属性在一个形式alter可能不工作,我记得看到一个问题,这个问题没有解决在那一刻。如果是这样,使用这个代码来附加ahah绑定,你不需要efect,method或progress,因为你只是想提交表单,而不是改变任何东西:

  function YOURMODULE_form_alter(& $ form,$ form_state,$ form_id){
if('YOURCONTENTTYPE_node_form'== $ form_id) {
//我可以使它为现有字段工作的唯一方法是添加绑定手动
$ ahah_binding = array(
'url'=> url('YOURCALLBACKPATH') ,
'event'=>'change',
'wrapper'=>'FIELD-wrapper',
'selector'=>'#FIELD',
'effect'=>'fade',
'method'=>'replace',
'progress'=> array('type'=>'throbber'),
);

drupal_add_js('misc / jquery.form.js');
drupal_add_js('misc / ahah.js');
drupal_add_js(array('ahah'=> array('FIELDd'=> $ ahah_binding)),'setting');

//强制表单被缓存
$ form ['#cache'] = TRUE;
}
}




  • 是我的回调函数,注意它从你发布的教程有一些修改:

      function YOURMODULE_js(){

    //形式是在包含文件中生成的,我们需要手动包含它。
    include_once'modules / node / node.pages.inc';
    //我们从步骤#3开始,准备#4。
    //如果没有创建空节点,我必须添加rebuild元素
    $ form_state = array('storage'=> NULL,'submitted'=> FALSE,'rebuild '=> TRUE);
    $ form_build_id = $ _POST ['form_build_id'];
    // Step#4。
    $ form = form_get_cache($ form_build_id,$ form_state);

    //准备#5。
    $ args = $ form ['#parameters'];
    $ form_id = array_shift($ args);
    $ form_state ['post'] = $ form ['#post'] = $ _POST;
    $ form ['#programmed'] = $ form ['#redirect'] = FALSE;

    //如果要对表单值进行任何修改,这是地方

    //步骤5。
    drupal_process_form($ form_id,$ form,$ form_state);
    //步骤#6和#7和#8。
    $ form = drupal_rebuild_form($ form_id,$ form_state,$ args,$ form_build_id);


    //最终呈现回调。
    drupal_json(array('status'=> TRUE,'data'=> $ output));
    }




<有详细资料,我已经忘了,但也许这会帮助你。



祝你好运。


I have a form that is added through the nodeapi displayed only on view mode. Users can select an item from a select menu and their choice will automatically get saved to the database with a hook_menu callback on change. If the user has javascript disabled, it'll submit normally with the form api. This is all working fine, but now for security reasons I want to submit the ajax version via the form api too. My form_name_submit is simple like:

function mymodule_test_form_submit($form, &$form_state) {
  global $user;
  db_query("INSERT INTO {mymodule} (nid, uid, status, created) VALUES (%d, %d, %d, " . time() . ")", $form['#parameters'][2], $user->uid, $form_state['values']['mymodule_status']);
}

My ajax:

$('.mysubmit').css('display', 'none');
$('.myclass').change(function() {
  $.ajax({
    type: 'POST',
    url: Drupal.settings.basePath + 'mymodule/set/' + $nid + '/' + $('.myclass').val(),
    dataType: 'json',
    data: { 'ajax' : true, 'form_build_id' : $('#mymodule-form input[name=form_build_id]').val() }
  });
});

And my callback function:

function mymodule_set($nid, $status) {
  $form_build_id = $_POST['form_build_id'];
  $form_state = array('storage' => NULL, 'submitted' => FALSE);
  $form = form_get_cache($form_build_id, $form_state);
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form['#post'] = $_POST;
  $form['#redirect'] = FALSE;
  $form['#programmed'] = FALSE;
  $form_state['post'] = $_POST;
  drupal_process_form($form_id, $form, $form_state);
}

Originally my callback function was about the same as my submit function, but now I'm trying to use the submit function with ajax as well. Is this the right way to use drupal_process_form? The form is grabbed from the cache, it get's validated and submitted if no errors? I'm using some code from this tutorial to apply to my situation: http://drupal.org/node/331941 There doesn't seem to be any examples of what I'm trying to do. I also have $form['#cache'] = TRUE; in my form function.

How does drupal_process_form compare the submitted values with the original form to check for integrity? Am I supposed to add my values to the form_state since the form state will be empty with ajax. Been stuck on this for a few days, hopefully someone has experience with this.

Thanks.

解决方案

I had to do somenthing similar to you in the past and read the same tutorial you posted, unfortunately there isn't much information avalaible about this and it was headache for me to make it work. I don't remember well the details but I was taking a look to the code I wrote and here are a couple of suggestions that may work for you:

IF you are doing this in a node form, adding the #ahah property in a form alter may not work, I remember having seen an issue about this that wasn't solved at that moment. if this is the case, use this code for attaching the ahah binding, you'll not need "efect", "method" or "progress" since you just want to submit the form, not to change anything about it:

function YOURMODULE_form_alter(&$form, $form_state, $form_id) {
  if ('YOURCONTENTTYPE_node_form' == $form_id) {
    //the only way I could make it work for exisiting fields is adding the binding "manually"
    $ahah_binding = array(
      'url'   => url('YOURCALLBACKPATH'), 
      'event' => 'change',
      'wrapper' => 'FIELD-wrapper',
      'selector' => '#FIELD',
      'effect'   => 'fade',
      'method'   => 'replace',
      'progress' => array('type' => 'throbber'),
    );

    drupal_add_js('misc/jquery.form.js');
    drupal_add_js('misc/ahah.js');
    drupal_add_js(array('ahah' => array('FIELDd' => $ahah_binding)), 'setting');

    //force the form to be cached
    $form['#cache'] = TRUE;
  }
}

  • Here is my callback function, note that it has some modifications from the tutorial you posted:

    function YOURMODULE_js() {
    
      // The form is generated in an include file which we need to include manually.
      include_once 'modules/node/node.pages.inc';
      // We're starting in step #3, preparing for #4.
      //I have to add the 'rebuild' element, if not an empty node was created
      $form_state = array('storage' => NULL, 'submitted' => FALSE, 'rebuild' => TRUE);
      $form_build_id = $_POST['form_build_id'];
      // Step #4.
      $form = form_get_cache($form_build_id, $form_state);
    
      // Preparing for #5.
      $args = $form['#parameters'];
      $form_id = array_shift($args);
      $form_state['post'] = $form['#post'] = $_POST;
      $form['#programmed'] = $form['#redirect'] = FALSE;
    
      // if you want to do any modification to the form values, this is the place 
    
      // Step #5.
      drupal_process_form($form_id, $form, $form_state);
      // Step #6 and #7 and #8.
      $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
    
    
      // Final rendering callback.
      drupal_json(array('status' => TRUE, 'data' => $output));
    }
    

As I said before there are details that I have forgot but maybe this will help you.

Good Luck.

这篇关于帮助ajax回调和drupal_process_form的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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