[英]action.php for a website send message form
我在网站上有一个发送消息的表格,消息应该发送到 email 地址。
由于我是网站建设的新手,所以我正在玩,但无法让表格与 action.php 文件一起使用。 当我将 HTML 代码和 action-page.php 上传到服务器(在同一子文件夹中)时,我在网站上键入的消息根本没有发送。 我没有收到 email,当我单击发送按钮时,我被转发到浏览器中的空白页面。 最好我只停留在同一页面上并收到“您的消息已发送”通知,但这是一个“不错的选择”,我只是想让它正常工作。
我使用: 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 文件,用于将实际消息发送到 email 地址。
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 文件(消息表单所在的位置)放在同一文件夹中。 那是正确的地方吗? 或者我是否需要将文件放在其他地方才能正确调用?
您已正确完成所有操作,但您错过了输入标签中的 name 属性。
你的代码应该是这样的,
<input class="c-form__field" id="contact-name" name="contact-name" type="text" placeholder="Bijvoorbeeld, Jade van de Ven" required>
您必须像上面的代码一样在所有输入标签中添加名称属性...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.