1

我正在用 HTML 编写一个 Intranet 站点,以及哪些在线用户能够添加他们自己的信息(例如 Skype 名称),这样我就不必亲自为公司的 1000 人添加这些信息,并且也这样当新的初学者进来时,他们可以自己添加自己的信息。

我目前有一个“用户”页面,其中包含指向另一个页面的链接,其中有一个表单代码,如下所示:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>Untitled Document</title>
     </head>

    <body>
     <form id="form1" name="form1" method="post" action="">
     <label>Please type in a message
     <input type="text" name="msg" id="msg" />
     </label>
     <label>and your name
     <input type="text" name="name" id="name" />
     </label>

     <p>
     <label>Submit
     <input type="submit" name="submit" id="submit" value="Submit" />
     </label>
     </p>
     </form>

     <?php
        $msg = $_POST["msg"];
        $name = $_POST["name"];
        $posts = file_get_contents("users.html");
        $posts = "$msg - $name\n" . $posts;
        file_put_contents("users.html", $posts);
        echo $posts;
     ?>

我希望将用户输入此表单的信息自动放入“users.html”页面上的列表中,每个人都可以看到。我曾希望上面的代码能实现这一点,但我无法让它工作。另外,我将如何在“users.html”页面中指定信息所在的位置?我希望它属于:

  <div id="content">
    <div class="content_item">

..coding,以便加载到网页内的列表中。

提前谢谢了 :)

4

2 回答 2

0

尽管数据库的使用会更好,并且文件会随着时间的推移而变得相当大,但我会根据您发布的代码回答您的实际问题。

要显示从表单写入的值(与下面分开),您可以在其中设置一个页面:

<div id="content">
<?php echo file_get_contents("users.html"); ?>
<div class="content_item">

单独的页面形式如下(经过测试)

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Title Here</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>

<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>

</body>
</html>

<?php
if(isset($_POST['submit']) && empty($_POST['msg']) && empty($_POST['name']) ) {
die("All fields need to be filled, please try again.");
}

if(isset($_POST['submit'])) {
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = $msg . "-" . $name;
$file = "users.html";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $posts . "\n<br>") or die("Couldn't write values to file!");
fclose($fp);
}

echo $posts;
echo "<hr>Previous messages<hr>";
$file = "users.html";

if (file_exists($file)) {
$fp = file_get_contents($file, "r") or die("Couldn't open $file for writing!");
echo $fp;
exit;
}

else {
echo "The file is empty";
}

?>
于 2013-09-09T14:36:37.913 回答
0

不要尝试在users.html每次提交时进行编辑。那就是疯狂。

将提交的数据存储在数据库中。使用PDO 库来执行此操作。

让显示该数据的页面按需从数据库中提取数据。

于 2013-09-09T11:41:55.590 回答