<?php
function MYCALLBACK_form($form_state) {
$form = array();
// add your form elements here. for example:
$form['MYTEXTFIELD'] = array(
'#type' => 'textfield',
'#title' => t('MY TEXT FIELD'),
);
// add submit button
$form['submit'] = array(
'#value' => 'Submit',
'#type' => 'button'
);
// add cancel button
// NOTE: the cancel button simply closes the lightbox window
$form['cancel'] = array(
'#value' => 'Cancel',
'#type' => 'button',
'#attributes' => array(
'onClick' => "Lightbox.end('forceClose'); return false;"
)
);
return $form;
}
?>
3,使用lightbox
<?php
// NOTE: you should use the l() function here to properly generate your A tag
$page_contents .= '<a href="/MY/URL" rel="lightmodal">MY LINK TEXT</a>';
?>
function my_js_submit_function(whichThis) {
// validate your form here
// define arguments to pass to ajax page callback
// you'll actually want to collect the user submitted form data here
var args = {
myArg1: 'blah',
myArg2: 'blah',
};
// submit the data using jQuery
// in this case, I'm using the getJSON function which uses GET and expects a JSON object in return
$.getJSON('/MY/URL/SUBMIT', args,
function(json){
// check for a return status, show a message, close the lightbox window, etc
alert(json.message);
if (json.status == true) {
Lightbox.end('forceClose');
}
}
);
}
需要一个最终页面的回调,并且增加义工菜单项来处理AJAX
<?php
function MYMODULE_menu() {
// ...code...
$items['MY/URL/SUBMIT'] = array(
'page callback' => 'MYMODULE_CALLBACK_SUBMIT',
'type' => MENU_CALLBACK,
);
// ...code...
}
function MYMODULE_CALLBACK_SUBMIT() {
// 1. validate $_GET (not shown, use your imagination)
// 2. process $_GET (not shown, use your imagination)
// 3. return a JSON object
// create return object
$returnO = new StdClass();
$returnO->status = true;
$returnO->message = 'MY INFORMATIVE MESSAGE HERE';
// output json result
print drupal_json($returnO);
// NOTE: if you don't die here, then the theme will be processed. we only want to return a JSON object
die;
}
?>
最后,我们得到的是一个链接,打开了一个lightbox窗口,包含一个表单,是通过ajax提交。
<?php
if ($title) print "<h1>$title</h1>";
if ($messages) print $messages;
if ($help) print $help;
if ($content) print $content;
?>
2 个回答
你可以安装lightbox2,借助 它,通过用lightbox2的lightmodal,实现弹出的表单。
这里顺便给出做表单的方法:
1,通过 hook_menu 创建地址
2,创建form
3,使用lightbox
Ajax返回
定义一个javascript函数来验证和使用AJAX提交表单。
需要一个最终页面的回调,并且增加义工菜单项来处理AJAX
最后,我们得到的是一个链接,打开了一个lightbox窗口,包含一个表单,是通过ajax提交。
分头诗人Lv 17
谢谢啦!诗人!。。。
editor_aaLv 9