0

我的 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。

4

3 回答 3

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>

(没有action属性意味着“提交到当前 URL”。)

然后将您的 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>";
    }
?>
于 2013-04-26T16:38:47.567 回答
2

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

于 2013-04-26T16:39:35.153 回答
0
If $sender = $recipient;

那么如何知道谁访问了该站点并订阅了该页面。相反,发件人邮件必须是订阅时事通讯的人的电子邮件地址。

于 2020-06-12T12:26:20.237 回答