php xml執行個體 留言本_php執行個體

來源:互聯網
上載者:User
複製代碼 代碼如下:

<?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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.