惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - xxp

翻译:Android程序开发入门 php,mysql下中文编码解决方案 asp.net中使用ajax中的三种方式 翻译:xoops搜索功能的使用 - xxp 发布新软件:史上最轻量级的ORM--EasyDBOperation XOOPS模块开发快速入门中文翻译--索引 - xxp XOOPS模块开发快速入门中文翻译(十) XOOPS模块开发快速入门中文翻译(九) XOOPS模块开发快速入门中文翻译(七) XOOPS模块开发快速入门中文翻译(六) XOOPS模块开发快速入门中文翻译(五) XOOPS模块开发快速入门中文翻译(四) XOOPS模块开发快速入门中文翻译(三) XOOPS模块开发快速入门中文翻译(二) XOOPS模块开发快速入门中文翻译(一) 简单的网页内容采集器(C#) C#网络编程学习笔记1 100%男性倾向? 55 为什么? 学习XOOPS的记录
XOOPS模块开发快速入门中文翻译(八)
xxp · 2008-04-19 · via 博客园 - xxp
 

  由于这两天一直研究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的效果。

Part 4 - Xoops Blocks