到目前为止我所做的是使用eclipse构建了一个基于java的web服务器。我可以访问我创建的index.html页面。现在我正在尝试创建一个联系表单,用户可以在其中输入信息并提交写入和修改文本文件,清除表单并显示用户在页面底部提交的数据任何建议。
注意:根据请求,我编辑了这篇文章以包含代码。
<?php
$saving = $_REQUEST['saving'];
if ($saving == 1){
$data = $_POST['data'];
$file = "data.txt";
$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
echo "Saved to $file successfully!";
}
?>
<form name="form1" method="post" action="data.php?saving=1">
<textarea name="data" cols="100" rows="10">
Name:
Address:
Email:
Phone:
---------------------------------------------
</textarea>
<br>
<input type="submit" value="Save">
</form>
<p>
<a href="data.txt"><b>VIEW<b></a>
答案 0 :(得分:0)
我也更新了你的一些HTML,所以请检查一下。 你正在做的是在这段代码中一次又一次地重写整个文件,删除bug。
<?php
if (isset($_POST['submit'])) {
$writeComment = implode('', file('test.txt')) . '[!@X#$]'. $_POST['data'];
$myfile = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($myfile, rtrim($writeComment, '[!@X#$]'));
fclose($myfile);
}
$fileContent = implode('', file('test.txt'));
$comments = explode('[!@X#$]', rtrim($fileContent, '[!@X#$]'));
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8" />
<title>Submit Data</title>
</head>
<body>
<form id="input" action="" method="post">
Comment: <textarea name="data" cols="100" rows="10">
Name:
Address:
Email:
Phone:
---------------------------------------------
</textarea>
<input type="submit" name="submit" value="Submit">
</form>
<table>
<?php foreach ($comments as $i => $comment) { ?>
<tr>
<td>
Comment(<?php echo ($i + 1)?>):
</td>
<td>
<?php echo $comment;?>
</tr>
</td>
<?php }?>
</table>
<a href="test.txt">View Core File</a>
</body>
</html>