欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php xml实例 留言本

程序员文章站 2023-09-08 22:53:44
复制代码 代码如下:
复制代码 代码如下:

<?php
//打开用于存储留言的xml文件
$guestbook = simplexml_load_file('db/guestbook.xml');

foreach($guestbook->thread as $th) //循环读取xml数据中的每一个thread标签
{
echo "<b>标题:</b>".$th->title."<br>";
echo "<b>作者:</b>".$th->author."<br>";
echo "<b>内容:</b><pre>".$th->content."</pre>";
echo "<hr>";
}
?>

复制代码 代码如下:

<?php
$guestbook = new domdocument(); //创建一个新的dom对象
$guestbook->load('db/guestbook.xml'); //读取xml数据
$threads = $guestbook->documentelement; //获得xml结构的根
//创建一个新thread节点
$thread = $guestbook->createelement('thread');
$threads->appendchild($thread);
//在新的thread节点上创建title标签
$title = $guestbook->createelement('title');
$title->appendchild($guestbook->createtextnode($_post['title']));
$thread->appendchild($title);
//在新的thread节点上创建author标签
$author = $guestbook->createelement('author');
$author->appendchild($guestbook->createtextnode($_post['author']));
$thread->appendchild($author);
//在新的thread节点上创建content标签
$content = $guestbook->createelement('content');
$content->appendchild($guestbook->createtextnode($_post['content']));
$thread->appendchild($content);
//将xml数据写入文件
$fp = fopen("db/guestbook.xml", "w");
if(fwrite($fp, $guestbook->savexml()))
echo "留言提交成功";
else
echo "留言提交失败";
fclose($fp);
?>

复制代码 代码如下:

<!doctype html public "-//w3c//dtd html 4.01 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<title>发表新的留言</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<h1><p align="center">发表新的留言</p></h1>
<form name="form1" method="post" action="post.php">
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>标题</td>
<td><input name="title" type="text" id="title" size="50"></td>
</tr>
<tr>
<td>作者</td>
<td><input name="author" type="text" id="author" size="20"></td>
</tr>
<tr>
<td>内容</td>
<td><textarea name="content" cols="50" rows="10" id="content"></textarea></td>
</tr>
</table>
<p align="center">
<input type="submit" value="submit">
<input type="reset" value="reset">
</p>
</form>
</body>
</html>