Surance Center

XOOPS模块开发快速入门中文翻译(八)

   由于这两天一直研究XOOPS的模块,所以找到了这篇很好的模块开发快速入门。
看了以后,就兴致勃勃的来开发模块了,可是开发的过程中遇到一些问题。
应该是我看的太快了,要学而时习之啊。因此翻译在这里。
==============
作者:Surance Yin
邮箱:Suranceyin@yahoo.com.cn
主页:http://www.fltek.com.cn
=================

第六步- 插入数据

现在我们来创建一个让用户插入数据库的表单(参考第一章).
打开 tutorial/templates/tut_form.html. 输入:

<form id="myform" name="myform" method="post" action="index.php">
<table width="400" border="0">
<tr>
<td align="right"><{$smarty.const.TT_NAME}></td>
<td><input type="text" name="name"></td>
</tr><tr>
<td align="right"><{$smarty.const.TT_ADDRESS}></td>
<td><input type="text" name="address"></td>
</tr><tr>
<td align="right"><{$smarty.const.TT_TELEPHONE}></td>
<td><input type="text" name="tel"></td>
</tr><tr>
<td align="right"><{$smarty.const.TT_EMAIL}></td>
<td><input type="text" name="email"></td>
</tr><tr>
<td></td>
<td><input type="button" value="Submit" onclick="xajax_processFormData(xajax.getFormValues('myform'));" /></td>
</tr>
</table>
</form>

打开 tutorial/functions.php. 输入:

<?php

require_once XOOPS_ROOT_PATH . '/class/template.php';
if (!isset($xoopsTpl)) {
$xoopsTpl = new XoopsTpl();
}
$xoopsTpl->xoops_setCaching(0);


function processFormData($arg)
{
// do some stuff based on $arg like query data from a database and
// put it into a variable like $newContent
$newContent = "Button pressed";

// Instantiate the xajaxResponse object
$objResponse = new xajaxResponse();

// add a command to the response to assign the innerHTML attribute of
// the element with id="SomeElementId" to whatever the new content is
$objResponse->assign("thisID","innerHTML", $newContent);

//return the xajaxResponse object
return $objResponse;
}

function showForm()
{
global $xoopsTpl;

$text = $xoopsTpl->fetch('db:tut_form.html');

$objResponse = new xajaxResponse();
$objResponse->assign("formDiv","innerHTML",$text);
return $objResponse;
}

?>

require_once XOOPS_ROOT_PATH . '/class/template.php';
if (!isset($xoopsTpl)) {
$xoopsTpl = new XoopsTpl();
}
$xoopsTpl->xoops_setCaching(0);
这一行对于2.0.x 系列版本的xoops (我不清楚 2.2.x)是必须的。 问题是:在tutorial/index.php. xoops 2.2 $xoopsTpl是在require('http://www.cnblogs.com/mainfile.php')定义的; 而 xoops 2.0.x 是在require(XOOPS_ROOT_PATH.'/header.php')定义的。这就是说,在 2.0.17 版本中开发的话, xajax 代码中需要获取$xoopsTpl解决方案是我们自己声明 $xoopsTpl .这就是这几行要做的事情。第一行引入了包含 $xoopsTpl类的文件。然后通过isset看是否设置过 $xoopsTpl变量。如果没有则声明它。然后取消缓存,随时更新模板。这样我可以随时改变模板内容,还不怕无法更新缓存。最后发布的时候,可以取消这一行。

$newContent = "Button pressed";对于xajax是必要的,因为即使是notice的报错,也会是xajax出错。有问题的话,可以用 $xajax->setFlag("debug", true);来调试。
function showForm()
{
global $xoopsTpl;
$text = $xoopsTpl->fetch('db:tut_form.html');创建了一个函数showForm 声明$xoopsTpl为全局变量,这样才可以在函数里面使用。开始有趣了:我创建爱了一个$text变量,这个变量抓取了tut_form.html模板。(即模板中包含模板)。

objResponse->assign("formDiv","innerHTML",$text);变量$text里面含有另外一个模板,现在把它分配给div


打开 tutorial/index.php.输入:

<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');

require_once(XOOPS_ROOT_PATH.'/modules/tutorial/functions.php');
require_once(XOOPS_ROOT_PATH.'/modules/tutorial/class/xajax/xajax_core/xajax.inc.php');

$xajax = new xajax();
$xajax->registerFunction("processFormData");
$xajax->registerFunction("showForm");
//$xajax->setFlag("debug", true);
$xajax->processRequest();

$Xjavapath=XOOPS_URL.'/modules/tutorial/class/xajax';
$xajaxjava=$xajax->getJavascript($Xjavapath);

$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');
$xoopsTpl->assign('xajaxjava', $xajaxjava);

require(XOOPS_ROOT_PATH.'/footer.php');
?>

$xajax->registerFunction("showForm");新加了一个函数的注册。


打开 tutorial/templates/tut_main.html. 输入

<p><{$xajaxjava}></p>
<p><a href="#" onclick="xajax_showForm();return false;">formtest</a></p>
<p><div id="formDiv"></div></p>
<div id="thisID"></div>

<a href="#" onclick="xajax_showForm();return false;">formtest</a>新加的一行可以调用showForm函数。return false;停止响应

测试一下。打开tutorial 主页面,点击 formtest. 会出现一个表单,点击写有 submit的按钮,可以看到新的一行,写着: Button pressed.所以,我们现在有2个div来显示2个输出。

第七步- 输入数据库

下一步我们将表单内容存入数据库

打开tutorial/functions.php 输入:

<?php

require_once XOOPS_ROOT_PATH . '/class/template.php';
if (!isset($xoopsTpl)) {
$xoopsTpl = new XoopsTpl();
}
$xoopsTpl->xoops_setCaching(0);


function processFormData($arg)
{
// do some stuff based on $arg like query data from a database and
// put it into a variable like $newContent
$newContent = addClient($arg);

// Instantiate the xajaxResponse object
$objResponse = new xajaxResponse();

// add a command to the response to assign the innerHTML attribute of
// the element with id="SomeElementId" to whatever the new content is
$objResponse->assign("thisID","innerHTML", $newContent);

//return the xajaxResponse object
return $objResponse;
}

function showForm()
{
global $xoopsTpl;

$text = $xoopsTpl->fetch('db:tut_form.html');

$objResponse = new xajaxResponse();
$objResponse->assign("formDiv","innerHTML",$text);
return $objResponse;
}

function addClient($data)
{
global $xoopsDB;
$myts = myTextSanitizer::getInstance();
$name=$myts->addslashes($data['name']);
$address=$myts->addslashes($data['address']);
$tel=$myts->addslashes($data['tel']);
$email=$myts->addslashes($data['email']);
$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";
$res=$xoopsDB->query($query);
if(!$res) {
$msg="error: $query";
} else {
$msg="Data was correctly inserted into DB!";
}
return $msg;
}

?>

调用了函数 addClient,这个函数从 tutorial_myform表单提取数据插入了数据库。相应的,函数 processFormData也改变了。这个函数会返回只是插入是否成功的 $msg。$msg 会显示在 <div> thisID中。 测试一下!

第八步-把users列表显示

现在我们把数据库里面的user全部显示出来。
打开 tutorial/xoops_version.php 并 改变模板处:

// Templates
$modversion['templates'][1]['file'] = 'tut_form.html';
$modversion['templates'][1]['description'] = '';

$modversion['templates'][2]['file'] = 'tut_client_list.html';
$modversion['templates'][2]['description'] = '';

$modversion['templates'][3]['file'] = 'tut_main.html';
$modversion['templates'][3]['description'] = '';

创建一个新的文件: tutorial/templates/tut_client_list.html 输入:

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><{$smarty.const.TT_NAME}></td>
<td><{$smarty.const.TT_ADDRESS}></td>
<td><{$smarty.const.TT_TELEPHONE}></td>
<td><{$smarty.const.TT_EMAIL}></td>
</tr>
<{foreach item=cli from=$clients}>
<tr>
<td><{$cli.name}></td>
<td><{$cli.address}></td>
<td><{$cli.telephone}></td>
<td><{$cli.email}></td>
</tr>
<{/foreach}>
</table>

更新一下模块。
打开 tutorial/index.php输入

<?php
// Tutorial
// Created by KaotiK
require('http://www.cnblogs.com/mainfile.php');

require_once(XOOPS_ROOT_PATH.'/modules/tutorial/functions.php');
require_once(XOOPS_ROOT_PATH.'/modules/tutorial/class/xajax/xajax_core/xajax.inc.php');

$xajax = new xajax();
$xajax->registerFunction("processFormData");
$xajax->registerFunction("showForm");
$xajax->registerFunction("listClients");
//$xajax->setFlag("debug", true);
$xajax->processRequest();

$Xjavapath=XOOPS_URL.'/modules/tutorial/class/xajax';
$xajaxjava=$xajax->getJavascript($Xjavapath);

$xoopsOption['template_main'] = 'tut_main.html';
require(XOOPS_ROOT_PATH.'/header.php');
$xoopsTpl->assign('xajaxjava', $xajaxjava);

require(XOOPS_ROOT_PATH.'/footer.php');
?>

打开 tutorial/functions.php输入:

<?php

require_once XOOPS_ROOT_PATH . '/class/template.php';
if (!isset($xoopsTpl)) {
$xoopsTpl = new XoopsTpl();
}
$xoopsTpl->xoops_setCaching(0);

function processFormData($arg)
{
// do some stuff based on $arg like query data from a database and
// put it into a variable like $newContent
$newContent = addClient($arg);

// Instantiate the xajaxResponse object
$objResponse = new xajaxResponse();

// add a command to the response to assign the innerHTML attribute of
// the element with id="SomeElementId" to whatever the new content is
$objResponse->assign("thisID","innerHTML", $newContent);

//return the xajaxResponse object
return $objResponse;
}

function showForm()
{
global $xoopsTpl;

$text = $xoopsTpl->fetch('db:tut_form.html');

$objResponse = new xajaxResponse();
$objResponse->assign("formDiv","innerHTML",$text);
return $objResponse;
}

function addClient($data)
{
global $xoopsDB;
$myts = myTextSanitizer::getInstance();
$name=$myts->addslashes($data['name']);
$address=$myts->addslashes($data['address']);
$tel=$myts->addslashes($data['tel']);
$email=$myts->addslashes($data['email']);
$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";
$res=$xoopsDB->query($query);
if(!$res) {
$msg="error: $query";
} else {
$msg="Data was correctly inserted into DB!";
}
return $msg;
}

function listClients(){
global $xoopsTpl;

$clients=clientLoader();
$xoopsTpl->assign('clients', $clients);

$text = $xoopsTpl->fetch('db:tut_client_list.html');

$objResponse = new xajaxResponse();
$objResponse->assign("clientListDiv","innerHTML",$text);
return $objResponse;

}

function clientLoader(){
global $xoopsDB;
$client=array();
$q=1;
$query = $xoopsDB->query(' SELECT * FROM ' . $xoopsDB->prefix('tutorial_myform'));
while($myrow = $xoopsDB->fetchArray($query) )
{
$client[$q]['name'] = $myrow['name'];
$client[$q]['address'] = $myrow['address'];
$client[$q]['telephone'] = $myrow['telephone'];
$client[$q]['email'] = $myrow['email'];
$q++;
}
return $client;
}

?>

打开 tutorial/templates/tut_main.html,输入

<p><{$xajaxjava}></p>
<p><a href="#" onclick="xajax_showForm();return false;">formtest</a></p>
<p><a href="#" onclick="xajax_listClients();return false;">List Clients</a></p>
<p><div id="formDiv"></div></p>
<p><div id="thisID"></div></p>
<p><div id="clientListDiv"></div></p>

测试一下!进入tutorial的主页面,有两个超链接,一个是formtest 另外一个是List Clients. 点击 List Clients. 可以看到我们刚才插入的数据.点击formtest会出现一个表单。这就是2个ajax的效果。

这里是完整的文件下载


Completed Module from part 3

Part 4 - Xoops Blocks

posted @ 2008-04-19 09:37  xxp  阅读(1076)  评论(1编辑  收藏  举报
Surance Center