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

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

问题描述

我有一个通过仅在视图模式下显示的 nodeapi 添加的表单.用户可以从选择菜单中选择一个项目,他们的选择将自动保存到数据库中,并在更改时使用 hook_menu 回调.如果用户禁用了 javascript,它将使用表单 api 正常提交.这一切正常,但现在出于安全原因,我也想通过表单 api 提交 ajax 版本.我的 form_name_submit 很简单:

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

我的阿贾克斯:

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

最初我的回调函数与我的提交函数大致相同,但现在我也尝试将提交函数与 ajax 一起使用.这是使用 drupal_process_form 的正确方法吗?表单是从缓存中获取的,如果没有错误,它会得到验证并提交?我正在使用本教程中的一些代码来适用于我的情况:http://drupal.org/node/331941 似乎没有任何关于我正在尝试做的事情的例子.我也有 $form['#cache'] = TRUE;在我的表单函数中.

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.

drupal_process_form 如何将提交的值与原始表单进行比较以检查完整性?我是否应该将我的值添加到 form_state 中,因为使用 ajax 时表单状态将为空.坚持了几天,希望有人有这方面的经验.

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.

谢谢.

推荐答案

过去我不得不做一些与您类似的事情并阅读您发布的同一教程,不幸的是没有太多关于此的信息,这很头疼让我让它发挥作用.我不太记得细节,但我正在查看我编写的代码,这里有一些可能对您有用的建议:

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:

如果您在节点表单中执行此操作,则在表单中添加 #ahah 属性可能无法正常工作,我记得我曾看到过一个关于此的问题,但当时尚未解决.如果是这种情况,请使用此代码附加 ahah 绑定,您将不需要效果"、方法"或进度",因为您只想提交表单,而不是对其进行任何更改:

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.

      祝你好运.

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

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