PHP / HTML新闻稿问题

时间:2013-04-26 16:31:49

标签: php html forms

我的WordPress网站上有一个简单的表单,允许用户注册简报:

HTML

<form method="POST" action="newsletter.php">
<input type="text" class="form" name="Email" size="20" placeholder="Email address">
<input type="submit" class="button" value="Submit" name="Submit"></p>
</form>

提交电子邮件地址后,用户将被重定向到名为newsletter.php的新网站,并显示以下消息:您的电子邮件地址已成功提交

PHP

<?php
$recipient = "blabla@mail.com";
$subject = "Newsletter subscription";
$sender = $recipient;
$body .= "Add this email adress to the newsletter: \n";
$body .= " ".$_REQUEST['Email']." \n";
mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
echo "Your email address has been successfully submitted";
?>

我现在想知道的是:如何在WordPress网站上显示“成功消息”而不是newsletter.php?

完美的解决方案是在注册表单下方打开一个小div。

3 个答案:

答案 0 :(得分:4)

您可以将表单更改为:

<form method="POST">
    <input type="text" class="form" name="Email" size="20" placeholder="Email address">
    <input type="submit" class="button" value="Submit" name="Submit"></p>
</form>

(No action属性表示“提交到当前网址”。)

然后将PHP添加到表单所在的同一页面,并添加一个检查,以便它只在表单提交后运行:

<?php
    if(isset($_POST['Email'])) {
        $recipient = "blabla@mail.com";
        $subject = "Newsletter subscription";
        $sender = $recipient;
        $body .= "Add this email adress to the newsletter: \n";
        $body .= " ".$_REQUEST['Email']." \n";
        mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
        echo "<div>Your email address has been successfully submitted</div>";
    }
?>

您可能还希望将错误处理策略更改为输出<div class="error">或某些而不是死亡。


我有一段时间没有使用Wordpress,所以我不确定那里的具体细节,但如果这是一个准系统的PHP网站,你可以在第一页的源代码中将它们全部放在一起:

<form method="POST">
    <input type="text" class="form" name="Email" size="20" placeholder="Email address">
    <input type="submit" class="button" value="Submit" name="Submit"></p>
</form>

<?php
    if(isset($_POST['Email'])) {
        $recipient = "blabla@mail.com";
        $subject = "Newsletter subscription";
        $sender = $recipient;
        $body .= "Add this email adress to the newsletter: \n";
        $body .= " ".$_REQUEST['Email']." \n";
        mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
        echo "<div>Your email address has been successfully submitted</div>";
    }
?>

答案 1 :(得分:2)

您可以将页面发布到自身并在那里处理订阅,而不是发布到Newsletter.php。或者Newsletter.php可能是无头的,并将您重定向回引用者,然后您可以在其中显示订阅消息。但这是相当陈旧的,任何Ajax后期解决方案肯定会更加性感。

答案 2 :(得分:0)

If $sender = $recipient;

然后如何知道谁访问了该网站并订阅了该页面。 相反,发件人邮件必须是订阅新闻通讯的人的电子邮件地址。