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

时间:2014-08-11 13:43:26

标签: php

出于某种原因,当我填写表单并单击“提交”时,它不会将信息放入.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文件?

1 个答案:

答案 0 :(得分:0)

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