php留言本xml版

content.xml
------------------------------------------------------------------------------
<?xml version="1.0" encoding="gb2312"?>
<notes>
</notes>

index.php
------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>留言板</title>
</head>
<body>
<form action="" method="post" name="postform" id="postform">
  <label for="head">头像链接地址:</label>
  <input  name="head" type="text" id="head"/>
  <label for="username">昵称:</label>
  <input  id="username" name="username" type="text" />
  <label for="url">个人主页:</label>
  <input name="url" type="text" id="url"/>
  <textarea name="message"></textarea>
  <input type="submit"  name="postsubmit" value="发表" />
</form>
<?php
if($_POST[postsubmit]){

$dom = new DOMDocument();//创建一个新的DOM对象
$dom->load("content.xml");//载入XML文件

$notes = $dom->documentElement;//获得XML结构的根

//notes节点下创建一个新note节点
$note = $dom->createElement("note");
$notes->appendChild($note);

//note节点下创建一个新num节点
$ob_num = $dom->getElementsByTagName("num");//取得文档中的所有num节点
$last_num = $ob_num->length;//取得num节点总数
$num_text = $last_num + 1;
$num = $dom->createElement("num");
$num_text = $dom->createTextNode($num_text);
$num->appendChild($num_text);
$note->appendChild($num);

//note节点下创建一个新head节点
$head = $dom->createElement("head");
$head_text = $dom->createTextNode($_POST["head"]);//创建一个新的文本节点
$head->appendChild($head_text);
$note->appendChild($head);

//note节点下创建一个新username节点
$username = $dom->createElement("username");
$username_text = $dom->createTextNode($_POST["username"]);
$username->appendChild($username_text);
$note->appendChild($username);

//note节点下创建一个新url节点
$url = $dom->createElement("url");
$url_text = $dom->createTextNode($_POST["url"]);
$url->appendChild($url_text);
$note->appendChild($url);

//note节点下创建一个新message节点
$message = $dom->createElement("message");
$message_text = $dom->createTextNode($_POST["message"]);
$message->appendChild($message_text);
$note->appendChild($message);

//note节点下创建一个新date节点
$date = $dom->createElement("date");
$date_text = date("Y-m-d");
$date_text = $dom->createTextNode($date_text);
$date->appendChild($date_text);
$note->appendChild($date);

//note节点下创建一个新time节点
$time = $dom->createElement("time");
$time_text = date("H:i:s");
$time_text = $dom->createTextNode($time_text);
$time->appendChild($time_text);
$note->appendChild($time);

//将XML数据写入文件
$fp = fopen("content.xml", "w"); //打开XML文件,写
if(fwrite($fp, $dom->saveXML())){
    echo "留言提交成功";}
else{
    echo "留言提交失败";}
fclose($fp);//关闭XML文件
}

?>
</body>
</html>

    A+
发布日期:2009年10月13日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: