Discuz教程网

PHP xml实例 留言本

[复制链接]
authicon dly 发表于 2011-9-5 20:30:04 | 显示全部楼层 |阅读模式
代码如下:

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

  4. foreach($guestbook->thread as $th) //循环读取XML数据中的每一个thread标签
  5. {
  6. echo "<B>标题:</B>".$th->title."<BR>";
  7. echo "<B>作者:</B>".$th->author."<BR>";
  8. echo "<B>内容:</B><PRE>".$th->content."</PRE>";
  9. echo "<HR>";
  10. }
  11. ?>
复制代码




代码如下:

  1. <?php
  2. $guestbook = new DomDocument(); //创建一个新的DOM对象
  3. $guestbook->load('DB/guestbook.xml'); //读取XML数据
  4. $threads = $guestbook->documentElement; //获得XML结构的根
  5. //创建一个新thread节点
  6. $thread = $guestbook->createElement('thread');
  7. $threads->appendChild($thread);
  8. //在新的thread节点上创建title标签
  9. $title = $guestbook->createElement('title');
  10. $title->appendChild($guestbook->createTextNode($_POST['title']));
  11. $thread->appendChild($title);
  12. //在新的thread节点上创建author标签
  13. $author = $guestbook->createElement('author');
  14. $author->appendChild($guestbook->createTextNode($_POST['author']));
  15. $thread->appendChild($author);
  16. //在新的thread节点上创建content标签
  17. $content = $guestbook->createElement('content');
  18. $content->appendChild($guestbook->createTextNode($_POST['content']));
  19. $thread->appendChild($content);
  20. //将XML数据写入文件
  21. $fp = fopen("DB/guestbook.xml", "w");
  22. if(fwrite($fp, $guestbook->saveXML()))
  23. echo "留言提交成功";
  24. else
  25. echo "留言提交失败";
  26. fclose($fp);
  27. ?>
复制代码




代码如下:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>发表新的留言</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  7. </head>
  8. <body>
  9. <H1><p align="center">发表新的留言</p></H1>
  10. <form name="form1" method="post" action="Post.php">
  11. <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  12. <tr>
  13. <td>标题</td>
  14. <td><input name="title" type="text" id="title" size="50"></td>
  15. </tr>
  16. <tr>
  17. <td>作者</td>
  18. <td><input name="author" type="text" id="author" size="20"></td>
  19. </tr>
  20. <tr>
  21. <td>内容</td>
  22. <td><textarea name="content" cols="50" rows="10" id="content"></textarea></td>
  23. </tr>
  24. </table>
  25. <p align="center">
  26. <input type="submit" value="Submit">
  27. <input type="reset" value="Reset">
  28. </p>
  29. </form>
  30. </body>
  31. </html>
复制代码







上一篇:PHP获取类中常量,属性,及方法列表的方法
下一篇:Discuz X2 头像无法显示
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-6-17 06:02

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表