【发布时间】:2014-08-10 14:26:21
【问题描述】:
我有 2 个文件:
- Index.html
- sendmail.php
代码运行良好,但我试图在同一页面上回显消息,而不是翻转到另一个页面或自动重定向到 index.html。
我尝试了很多选项,但都没有成功。我需要一些帮助 - 我是 php 新手。
代码如下:
索引.html:
<form class="form-inline" action="php/sendmail.php" method="post">
<input type="text" name="email" placeholder="Please enter your email...">
<button type="submit" class="btn_email">Subscribe</button>
</form>'
sendmail.php:
<?php
$to = "info@mywebsite.com"; // my email address
$from = "no-reply@mywebsite.com"; // from (my email)
$headers = "From: " . $from . "\r\n";
$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{
if (mail($to, $subject, $body, $headers, "-f " . $from))
{
$filename=$_SERVER["PHP_SELF"];
echo 'Your e-mail (' . $_POST['email'] . ') has been added to our mailing list!';
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
}
else
{
echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';
}
【问题讨论】:
-
“我正在尝试在同一页面上回显消息” - 不是
.html文件扩展名,除非你指示 Apache将.html文件视为 PHP。