所以我最近将我的第一个网站上传到iPage服务器。该网站运行完美,但联系表格除外,因为某些原因拒绝从表格中发送任何电子邮件。 我使用Post方法的PHP脚本,并尝试多次修复我的代码以纠正我可能输入的任何错误,但到目前为止无济于事。这是代码:
HTML: <
form action = "js/mailer.php" form method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "aravindm3095@gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
mail($to, $subject, $body);
// redirect to confirmation
header('Location: confirmation.html');
} else {
echo "Failure";
}
?>
有人可以帮我吗?我的托管服务器可能出错,或者我的代码出错。非常感谢您的帮助
其他评论:按照提供的帮助(谢谢你)我对HTML和PHP进行了必要的更改,但表单仍无法正常运行。它不会回显失败或重定向到确认页面,并且在使用Firefox检查元素时,我注意到在按下提交按钮时,其下面会出现一个细分,表示&#34;发送....&#34;。但是没有发送电子邮件,也没有回显任何消息或打开页面。
答案 0 :(得分:1)
从我在您发布的代码中看到的内容,PHP邮件脚本将不起作用,因为您正在检查是否存在名称为“submit”的POST变量,而不是表单中的提交按钮没有名称属性。 尝试给提交按钮一个名称,并将该名称放在PHP if语句中。
答案 1 :(得分:0)
表单中缺少提交按钮名称,您需要在提交按钮中添加名称
<input type="submit" name="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
^^^^^^^^^^^^
而不是
<input type="submit" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
答案 2 :(得分:0)
您的HTML部分应如下所示
<form action = "js/mailer.php" method="post" name="contactform" id="contactform" class="form validate item_bottom" role="form">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control required email" placeholder="Email">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control input-lg required" rows="9" placeholder="Enter Message"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="sendemail" id="contactForm_submit" class="btn btn-trans btn-border btn-full" value="Submit">
</div>
</form>
在上面,我在提交按钮中添加了name="sendemail"
。
发送电子邮件的PHP脚本应该像
<?php
if(isset($_POST['sendemail'])) { // <-- variable name changed
$to = "aravindm3095@gmail.com";
$subject = "Hello Aravind!";
// data the visitor provided
$name_field = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email_field = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$comment = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//constructing the message
$body = " From: $name_field\n\n E-Mail: $email_field\n\n Message:\n\n $comment";
$headers = "From: " . $email_field; // <-- Code added
mail($to, $subject, $body, $headers); // <-- Code added
// redirect to confirmation
header('Location: confirmation.html');
exit;
} else {
echo "Failure";
}
?>