帮助阿贾克斯回调drupal_process_form回调、阿贾克斯、drupal_process_form

2023-09-10 20:15:12 作者:妞,给爷满上

我有一个是通过只在视图模式中显示的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']);
}

我的AJAX:

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

和我的回调函数:

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?的形式从高速缓存抓起,它得到的验证并且如果没有错误提交?我使用了一些code本教程的适用于我的情况: http://drupal.org/node / 331941人们似乎并没有成为我想要做的任何实例。我也有$形式['#缓存'] = 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,因为形式的国家将是空的阿贾克斯。一直停留在这几天,希望有人有经验,与此有关。

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.

感谢。

推荐答案

我不得不这样做somenthing过类似的你和阅读您发布相同的教程,遗憾的是没有太多的信息avalaible关于这一点,这是头痛对我来说,使其工作。我不记得很清楚的细节,但我正在看向code我写在这里有几个建议,可能会为你工作:

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:

在使用自己的jQuery code不提交表单,而是使用了可用于调用回调在选择的http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#ahah

如果你这样做是在一个节点的形式,添加在表单中#ahah属性改变可能无法正常工作,我记得曾看过有关这一点,并没有解决在那一刻的问题。如果是这样的话,使用这个code,用于将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.

好运。

 
精彩推荐
图片推荐