PHP博客没有按预期将数据保存到文件中


PHP blog not saving data to file as expected

由于某些原因,当我填写表单并单击提交时,它没有将信息放入.txt文件中。

  <table id="hor-zebra" summary="Employee Pay Sheet">
    <thead>
        <tr>
            <th width="27%" scope="col">Name</th>
            <th width="31%" scope="col">Email</th>
            <th width="42%" scope="col">Comment</th>
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
        </tr>
        <tr>
        </tr>
        <tr class="odd">
        </tr>
        <tr>
        </tr>
        <?php  
    $blogFile = 'blog.txt';
    if (file_exists($blogFile))
    {
        $readfile = file($blogFile);
        for ($k=0; $k<=count($readfile)-1; $k++) {
            $fields = split("'t",$readfile[$k]);
            print("<tr>");
            print("<td>$fields[0]</td>");
            print("<td>$fields[1]</td>");
            print("<td>$fields[2]</td>");
            print("</tr>");
        }
    }
?>
    </tbody>
</table>
                                    <h2>Blog</h2>
                                    <form method="post" action="writeBlog.php">
                <div class="row half">
                    <div class="6u">
                        <input type="text" class="text" name="Name" id="Name" placeholder="Name" />
                    </div>
                    <div class="6u">
                        <input type="text" class="text" name="Email" id="Email" placeholder="Email" />
                    </div>
                </div>
                <div class="row half">
                    <div class="12u">
                        <textarea name="Comment" placeholder="Comment" id="Comment"></textarea>
                    </div>
                </div>
                <div class="row">
                    <div class="12u"> <a href="writeBlog.php" class="button">Submit comment</a> </div>
                </div>
            </form>

下面是处理from的PHP代码。

<?php 
$out = fopen("blog.txt", "a");
if (!$out) { 
    header('Location: blog.php');
    exit;
}
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Comment = $_POST['Comment'];
$Comment = str_replace("'n", " ", $Comment);    
fputs($out,"$Name't");
fputs($out,"$Email't");
fputs($out,"$Comment'n");
fclose ($out); 
    header('Location: blog.php');
    exit;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
</html>

为什么这段代码没有将信息放入.txt文件中?

这个<a href="writeBlog.php" class="button">Submit comment</a>不做你期望它做的事;使用一个真正的提交按钮,而不是一个href链接到文件。

代替

<a href="writeBlog.php" class="button">Submit comment</a>

<input type = "submit" name = "submit" value = "Submit comment">

表单的动作已经处理好了。

<form method="post" action="writeBlog.php">