如何更新Drupal 7 AJAX表单?

时间:2010-08-09 18:21:01

标签: ajax drupal forms

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

我可以使用Chrome开发者工具复制出来的最新错误:

阵列(       'term_name'=> 'jfidj',       'set_id'=> '1',     )     [     {         “命令”:“设置”,         “设置”:{             “基本路径”: “\ / drupal7 \ /”,             “覆盖”:{                 “路径”:{                     “管理”:“节点\ / * \ /编辑\ nnode \ / * \ /删除\ nnode \ / * \ /修改\ nnode \ / * \ /修改\ / * \ /复原\ nnode \ / * \ /修改\ / * \ /删除\ nnode \ /添加\ nnode \ /添加\ / * \ nadmin \ nadmin \ / * \ nbatch”                     “non_admin”: “”                     }, “ajaxCallback”: “叠加AJAX”                     }                     }, “合并”:假的                     },{                         “命令”:“插入”,                         “方法”:空,                         “选择”:空,                         “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 \ u003incput 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”,                         “设置”:空},                         {                             “命令”:“插入”,                             “方法”:“前置”,                             “选择”:空,                             “数据”:””,                             “设置”:空                             }                             ]

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

据我所知,我正在做正确的事情。我一直在关注示例模块和Drupal Ajax forms guide

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

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

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

/**
 * 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 个答案:

答案 0 :(得分:1)

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

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

无论如何,希望这可以帮助某人搜索类似的错误。

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