【发布时间】:2011-12-21 14:44:42
【问题描述】:
我想做的是:我想在带有视图的页面上呈现表单。此视图有一个“注释”列表 (=CT Note)。当您填写表格时,便条被存储,表格被清除,新的便条被添加到列表中。全部无需刷新页面。
我创建了一个模块new_note,并添加了这个功能:
function new_note_form($nodeid = NULL) {
ctools_include('ajax');
// drupal_add_js(drupal_get_path('module', 'custom_forms') . '/js/note_form.js');
//dpm($nodeid);
module_load_include('inc', 'node', 'node.pages');
$form = node_add('note');
$form['field_note_reference']['und']['#value'] = '2';
$form['field_note_reference']['und']['#validated'] = 'TRUE';
$form['field_note_reference']['#attributes']['class'][] = "hidden";
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#executes_submit_callback' => FALSE,
'#ajax' => array(
'callback' => 'ajax_note',
'wrapper' => 'status',
),
);
$output= drupal_render($form);
dpm($form);
print $output;
}
function ajax_note(&$form, &$form_state) {
return 'test';
}
我在显示套件块字段中使用此功能,该字段呈现在笔记列表上方。到目前为止一切顺利。
唯一的问题是,当我提交表单时,没有调用ajax,正常提交。
谁能帮帮我
@编辑。
根据 clive 的建议,我更改了代码,并让 ajax 正常工作。
function new_notes_form($nodeid = NULL) {
global $user;
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => 'note',
'language' => LANGUAGE_NONE,
);
$form_state = array();
$form_state['build_info']['args'] = array($node);
form_load_include($form_state, 'inc', 'node', 'node.pages');
$form = drupal_build_form('note_node_form', $form_state);
$form['field_note_reference']['und']['#value'] = '2';
$form['field_note_reference']['#attributes']['class'][] = "hidden";
$form['submit'] = array(
'#type' => 'button',
'#value' => 'Submit',
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'ajax_note_replace',
'wrapper' => 'status',
),
);
return $form;
}
function ajax_note_replace(&$form, &$form_state) {
dpm("test");
dpm($form);
$output = '<h1>' . t('Hello World') . '</h1>';
// $node = node_load('6');
// $output .= drupal_render(node_view($node, $style = 'teaser', $options = array()));
ctools_include('ajax');
$commands = array();
$commands[] = ajax_command_prepend(".view-content", $output);
print ajax_render($commands); // this function exits.
exit;
}
@Clive,你能帮我解决剩下的问题吗?我想在回调中保存节点(如果有效?)。在 ajax 回调中,我的节点 ID 为空,因为它尚未存储?如何验证和保存节点,否则将无效的表单项设置为正常红色。
【问题讨论】:
-
你能把
advanced_form_callback()的代码贴出来吗? -
抱歉版本有误,更新了
标签: php ajax forms drupal drupal-7