我正在創建一個Drupal 7模塊。目前我的目標是獲取表單以將某些內容插入到數據庫中,然後告訴用戶它已經工作。我可以把它提交給數據庫就好了。獲取表單重建並告訴用戶他們的條款已被提交將無法工作。我不斷收到這些警報錯誤變化:我將在後,由於垃圾郵件防禦評論.... 嘆息如何更新Drupal 7 AJAX表單?
最近的一個錯誤,我能夠與Chrome開發者工具複製出來:
陣列( 'term_name'=> 'jfidj', 'SET_ID'=> '1', ) [ { 「命令」: 「設置」, 「設置」:{ 「基本路徑」:「\/drupal7 \ /「, 」overlay「:{ 」paths「:{ 「admin」:「node \/* \/edit \ nnode \/* \/delete \ nnode \/* \/revisions \ nnode \/* \/revisions \/* \/revert \ nnode \/* \ /修改\/* \ /刪除\ nnode \ /添加\ nnode \ /添加\/* \ nadmin \ nadmin \/* \ nbatch「, 」non_admin「:」「 },」ajaxCallback「:」overlay-ajax「 } }, 「合併」:假 },{ 「命令」: 「插入」, 「方法」:空, 「選擇器」:空, 「數據」: 「\ 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 \ u003輸入類型= \「文本\」maxlength = \「128 \」name = \「message \」id = \「edit-message - 2 \」size = \「60 \」value = \「 \「class = \」form-text \「\/\ u003e \ n \ u003c \/div \ u003e \ n \ u003c \ /格\ u003e 「 」設置「 日期null}, { 」命令「: 」插入「, 」方法「: 」前置「, 」選擇器「:空, 」 數據「:」」, ‘設置’:空 } ]
我試圖把它更好的格式,但它只是凌亂......
至於我可以告訴我正確地做事情。我一直在關注示例模塊和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:http://develzone.aetwirk.com/test/ajaxerror.jpg – 2010-08-09 18:21:18
錯誤2:http://develzone.aetwirk.com/test/ajaxerror2.jpg – 2010-08-09 18:21:56
Drupal論壇主題:http:// drupal.org/node/872088 – 2010-08-09 18:22:27