如何更新Drupal 7 AJAX表单?

时间:2022-09-12 08:52:20

I am creating a Drupal 7 module. Currently my goal is to get the form to insert something into the database and then tell the user that it worked. I can get it to submit to the database just fine. Getting the form to rebuild and tell the user that their term has been submitted won't work. I keep getting variations on these alert errors: Which I will post in comments due to spam prevention.... sigh

我正在创建一个Drupal 7模块。目前我的目标是让表单在数据库中插入一些内容然后告诉用户它是否有效。我可以让它提交到数据库就好了。获取表单以重建并告知用户他们的术语已被提交将无效。我不断收到这些警报错误的变化:由于垃圾邮件的预防,我将在评论中发帖....叹息

A more recent error I was able to copy out with Chrome developer tools:

我最近使用Chrome开发人员工具复制的错误:

array ( 'term_name' => 'jfidj', 'set_id' => '1', ) [ { "command":"settings", "settings":{ "basePath":"\/drupal7\/", "overlay":{ "paths":{ "admin":"node\/*\/edit\nnode\/*\/delete\nnode\/*\/revisions\nnode\/*\/revisions\/*\/revert\nnode\/*\/revisions\/*\/delete\nnode\/add\nnode\/add\/*\nadmin\nadmin\/*\nbatch", "non_admin":"" },"ajaxCallback":"overlay-ajax" } },"merge":false },{ "command":"insert", "method":null, "selector":null, "data":"\u003cdiv id=\"form_message\"\u003e\u003cdiv class=\"form-item form-type-textfield form-item-message\"\u003e\n \u003clabel for=\"edit-message--2\"\u003ehidden \u003c\/label\u003e\n \u003cinput type=\"text\" maxlength=\"128\" name=\"message\" id=\"edit-message--2\" size=\"60\" value=\"\" class=\"form-text\" \/\u003e\n\u003c\/div\u003e\n\u003c\/div\u003e", "settings":null}, { "command":"insert", "method":"prepend", "selector":null, "data":"", "settings":null } ]

I tried to format it better, but it's just messy...

我试着更好地格式化它,但它只是凌乱......

As far as I can tell I am doing things correctly. I've been following the examples module, and the Drupal Ajax forms guide.

据我所知,我正在做正确的事情。我一直在关注examples模块和Drupal Ajax表单指南。

I create the form with wrappers defined, then have an if statement in there that only runs if the form has been submitted, then use ajax to replace the wrapped form element according to what you want.

我创建了定义了包装器的表单,然后在那里有一个if语句,只有在表单已经提交时运行,然后使用ajax根据你想要的内容替换包装的表单元素。

To see what all I have tried, you can look at the Drupal forum topic I will post a link to in the comments.

要查看我所尝试的内容,您可以查看Drupal论坛主题,我将在评论中发布链接。

Here's the current code I am trying, there's some code I've commented out, that I've tried and doesn't work.

这是我正在尝试的当前代码,我已经注释了一些代码,我已经尝试过但不起作用。

/**
 * Add a term
 */
function markit_form_term_add()
{
    $sets = markit_get_marksets();
    //drupal_set_message(var_export($sets));
    $form = array();

    $form['message'] = array(
        '#type' => 'textfield',// . !(empty($form_state['values']['term_name'])) ? 'textfield' :  'hidden',
        '#title' => t('hidden'),
        '#value' => '',// . !(empty($form_state['values']['term_name'])) ? 'Added term: ' . $form_state['values']['term_name'] :  'Message goes here.',
        '#prefix' => '<div id="form_message">',
        '#suffix' => '</div>',
    );
    $form['add'] = array(
        '#type' => 'fieldset',
        '#title' => t('Add new Term'),
        '#prefix' => '<div id="add_term_form">',
        '#suffix' => '</div>',
    );
    $form['add']['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Term name'),
        '#size' => 15,
    );
    //select the set you want to add the term to

    $setNames = array();
    foreach($sets as $row)
    {
        $id = $row['set_id'];
        $setNames[$id] = t($row['set_name']);
    }
    $form['add']['sets']['set_names'] = array(
        '#type' => 'select',
        '#title' => t('Select a set.'),
        '#options' => $setNames,
        '#description' => t('Select from the list of sets.'),
        );

    $form['add']['submit'] = array(
        '#type'  => 'button',
        '#value' => t('Add'),
        '#ajax' => array(
            'callback' => 'markit_ajax_terms_add_callback',
            'wrapper' => 'form_message',
            'method' => 'replace',
        ),
    );

    if(!empty($form_state['values']['name']))
    {
        $form['message']['#type'] = 'textfield';
        $form['message']['#value'] = t('Added term: ');// . $form_state['values']['term_name'];
    }

    return $form;
}

function markit_ajax_terms_add_callback($form, $form_state)
{    
    $entry = array(
       'term_name' => $form_state['values']['name'],
        'set_id' => $form_state['values']['set_names'],
        );
    markit_form_term_add_insert($entry);
    return $form['message'];    
}

/*
 * Get an array of mark sets
 */
function markit_get_marksets()
{
    $output = '';

    $select = db_select('markit_sets','s');
    $select->addField('s','set_id');
    $select->addField('s','set_name');
    $entries = $select->execute()->fetchAll(PDO::FETCH_ASSOC);
    return $entries;
}

/**
 * Get terms by set_id
 */
function markit_get_markterms($termid=NULL,$termname=NULL,$setid=NULL)
{
    $select = db_select('markit_terms','t');
    $select->addField('t','term_id');
    $select->addField('t','term_name');
    $select->addField('t','set_id');
    if($termid)
        $select->condition('term_id',$termid);//,'=');
    if($termname)
        $select->condition('term_name',$termname);//,'=');
    if($setid)
        $select->condition('set_id',$setid);//,'=');
    $entries = $select->execute()->fetchAll(PDO::FETCH_ASSOC);
return $entries;
}

/**
 * Insert new set into database
 */
function markit_form_set_add_insert($entry)
{
    $return_value = NULL;
  try {
    $return_value = db_insert('markit_sets')
                    ->fields($entry)
                    ->execute();
  }
  catch (Exception $e) {
    drupal_set_message(t('db_insert failed. Message = %message, query= %query',
      array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
  }
  return $return_value;
}

/**
 * Insert new set into database
 */
function markit_form_term_add_insert($entry)
{
    drupal_set_message(var_export($entry));
    $return_value = NULL;
  try {
    $return_value = db_insert('markit_terms')
                    ->fields($entry)
                    ->execute();
  }
  catch (Exception $e) {
    drupal_set_message(t('db_insert failed. Message = %message, query= %query',
      array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
  }
  return $return_value;
}

1 个解决方案

#1


1  

Right. In markit_form_term_add_insert, I had some code that I had been using to figure out some previous bugs (drupal_set_message(var_export($entry));). Deleting that fixed the problem. The debug code was the bug.... bangs head against wall

对。在markit_form_term_add_insert中,我有一些代码,我一直在用它来弄清楚以前的一些错误(drupal_set_message(var_export($ entry));)。删除修复问题。调试代码是bug .... bangs靠墙

Specifically, I was using drupal_set_message to view the contents of a variable and make sure they were correct. But when I started using ajax, that drupal_set_message wasn't being called correctly anymore. It's supposed be called after a form is submitted, or, apparently, returned by the ajax callback. If it's just called like you would outside of ajax, it gives those errors.

具体来说,我使用drupal_set_message来查看变量的内容并确保它们是正确的。但是当我开始使用ajax时,drupal_set_message不再被正确调用。它应该在提交表单后调用,或者显然由ajax回调返回。如果它只是像你在ajax之外那样被调用,它会给出那些错误。

Anyway, hope that this helps someone when they are searching for similar errors.

无论如何,希望这有助于某人在搜索类似错误时。

Basically, go through every single function that is being called. Even the ones that you know are not affecting anything. Somewhere in there is some code that isn't being called the way it's supposed to.

基本上,遍历每个被调用的函数。即使你认识的那些也没有影响任何东西。在某处某些代码没有按照预期的方式调用。

#1


1  

Right. In markit_form_term_add_insert, I had some code that I had been using to figure out some previous bugs (drupal_set_message(var_export($entry));). Deleting that fixed the problem. The debug code was the bug.... bangs head against wall

对。在markit_form_term_add_insert中,我有一些代码,我一直在用它来弄清楚以前的一些错误(drupal_set_message(var_export($ entry));)。删除修复问题。调试代码是bug .... bangs靠墙

Specifically, I was using drupal_set_message to view the contents of a variable and make sure they were correct. But when I started using ajax, that drupal_set_message wasn't being called correctly anymore. It's supposed be called after a form is submitted, or, apparently, returned by the ajax callback. If it's just called like you would outside of ajax, it gives those errors.

具体来说,我使用drupal_set_message来查看变量的内容并确保它们是正确的。但是当我开始使用ajax时,drupal_set_message不再被正确调用。它应该在提交表单后调用,或者显然由ajax回调返回。如果它只是像你在ajax之外那样被调用,它会给出那些错误。

Anyway, hope that this helps someone when they are searching for similar errors.

无论如何,希望这有助于某人在搜索类似错误时。

Basically, go through every single function that is being called. Even the ones that you know are not affecting anything. Somewhere in there is some code that isn't being called the way it's supposed to.

基本上,遍历每个被调用的函数。即使你认识的那些也没有影响任何东西。在某处某些代码没有按照预期的方式调用。