【问题标题】:Save Contact Form's Messages in Server在服务器中保存联系表单的消息
【发布时间】:2018-09-16 18:10:23
【问题描述】:

如何将联系人表单的消息保存在服务器中? 我使用 PHP 来保存消息,但它不起作用并出现“HTTP ERROR 405”错误。

我的 HTML 代码:

<body>
<form action="contact.php" method="POST">
    <p>Name</p><input type="text" name="name">
    <p>Email</p><input type="text" name="email">
    <p>Message</p><textarea name="message" rows="6" cols="25"></textarea><br/>
    <input type="submit" value="Send"><input type="reset" value="Clear">
</form>
</body>

我的 PHP 代码

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$myfile = fopen("textfile.txt", "a") or die("Unable to open file!");
fwrite($myfile, $name);
fwrite($myfile, $email);
fwrite($myfile, $message);
fclose($myfile);
?>

或者有没有更简单的方法在服务器中保存消息?

【问题讨论】:

  • 这可能是在服务器上存储数据的最简单(也是最不实用)的方法。问题是您的服务器未配置为处理 POST 请求。启用 POST 或改用 GET。
  • HTTP 405 错误是“方法不允许”。你在什么样的主机上运行?
  • @j08691 这只是一个本地实时服务器。

标签: php


【解决方案1】:

经过以下过程,问题解决了。

  1. 重新安装 LAMP (instruction video)
  2. 修改了 apache2.conf 文件 (instruction)
    设置目录 /var/www 的 AllowOverride
  3. 更改 /var/www/html 文件夹的权限(instruction1、instruction2)
  4. 将 html 和 php 文件放在 /var/www/html 文件夹中

最后,转到http://localhost/contact.html,联系表格可以将消息保存在文本文件中。

【讨论】:

    【解决方案2】:

    显示“405 Method Not Allowed”,因为您的服务器不支持 POST。 试试 GET 方法

    <body>
    <form action="contact.php" method="GET">
        <p>Name</p><input type="text" name="name">
        <p>Email</p><input type="text" name="email">
        <p>Messages</p><textarea name="messages" rows="6" cols="25"></textarea><br/>
        <input type="submit" value="Send"><input type="reset" value="Clear">
    </form>
    </body>
    

    php

    <?php
    $name = $_GET['name'];
    $email = $_GET['email'];
    $message = $_GET['message'];
    $myfile = fopen("messages.txt", "a") or die("Unable to open file!");
    fwrite($myfile, $name);
    fwrite($myfile, $email);
    fwrite($myfile, $message);
    fclose($myfile);
    ?>
    

    【讨论】:

    • 我可以使用 GET 方法,但它不会将消息保存在 message.txt 文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2013-09-04
    • 2014-10-11
    • 2022-01-13
    相关资源
    最近更新 更多