从html写入文本文件的简单联系人表单

Simple Contact form that writes to text file from html

本文关键字:简单 联系人 表单 文件 文本 html      更新时间:2023-09-26

到目前为止,我所做的是使用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>

我也更新了你的一些html,所以请也检查一下。你所做的是一次又一次地重写整个文件,在这段代码中,这个错误被删除了。

<?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>