【发布时间】:2020-05-17 07:27:47
【问题描述】:
我在网站上有一个表格来发送消息,消息应该发送到一个电子邮件地址。
由于我是网站建设的新手,我正在玩,但无法让表单与 action.php 文件一起使用。当我将 HTML 代码和 action-page.php 上传到服务器(在同一个子文件夹中)时,我在网站上输入的消息根本不会发送。我没有收到电子邮件,当我单击发送按钮时,我被转发到浏览器中的空白页面。最好我只是停留在同一页面上并收到“您的消息已发送”通知,但这是一个“不错的选择”,我只想让它工作。
我使用:https://html.form.guide/email-form/php-form-to-email.html 作为代码的参考。
1.出了什么问题或为什么它不起作用?
网站消息表单代码如下:
<div class="o-screen-contact__col-form">
<form class="c-form js-form-validation" action="action-page.php" method="post">
<label for="contact-name">Jouw naam</label>
<input class="c-form__field" id="contact-name" type="text" placeholder="Bijvoorbeeld, Jade van de Ven" required>
<label for="contact-email">Jouw e-mail</label>
<input class="c-form__field" id="contact-email" type="email" placeholder="Jouwemail@email.nl" required>
<label for="contact-message">Jouw bericht</label>
<textarea class="c-form__field" id="contact-message" name="name" placeholder="Schrijf hier jouw bericht of zorg" required></textarea>
<fieldset class="u-text-right">
<button class="c-button c-button--primary" type="submit" name="contact-submit">Zend mij een bericht</button>
</fieldset>
</form>
</div>
我使用了 post 方法并制作了一个 action-page.php 文件,以便将实际消息发送到电子邮件地址。
action-page.php 文件如下所示:
<?php
$name = $_POST['contact-name'];
$visitor_email = $_POST['contact-email'];
$message = $_POST['contact-message'];
?>
<?php
$email_from = 'zorg@ontzorg-zwolle.nl';
$email_subject = "Nieuwe email website ontzorg-zwolle";
$email_body = "You have received a new message from the user $contact-name.\n".
"Here is the message:\n $contact-message".
?>
<?php
$to = "zorg@ontzorg-zwolle.nl";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
2。将 action-page.php 文件放在服务器的什么位置
我已将 action-page.php 文件与 index.html 文件(消息表单所在的位置)放在同一个文件夹中。那是正确的地方吗?还是我需要将文件放在其他地方才能正确调用?
【问题讨论】:
-
通常情况下,您的服务器上的顶级文件夹中会有一个 index.php 或 html 页面。可能有一个 pages 文件夹,带有 css 文件夹,也许还有一个 javascript 文件夹。根据您的操作页面的放置位置,您可以使用
/在子文件夹中访问它。因此,如果您在顶级文件夹中有一个名为 index.php 的文件,并且它的形式正在向 pages 文件夹中的 action-page.php 发送帖子,则 action 属性看起来像;action="pages/action-page.php" -
如果您在服务器上的文件级别上升一级,它看起来像
../。所以说我有一个文件名customers.php,它位于服务器的第二级,并且有一个表单,该表单正在发布到一个名为user.php的文件,该文件位于includes文件夹中,该文件夹也是第二级服务器,我必须离开pages文件夹并上一层才能访问includes文件夹和随后的user.php我的操作看起来像:action="../includes/user.php" -
另外,通常在通过 post 处理表单数据时,您应该首先查看处理表单提交的提交按钮是否使用
isset()设置。if(isset($_POST['contact-submit'])){ //--> check and handle validate sanitize user input etc... }