PHP表单的动作和回显结果问题


PHP Form issues with action and echoing result

我正在尝试获得一个简单的双字段表单提交到电子邮件地址,然后在表单下面(或代替表单)回显"感谢注册您的兴趣"。

供参考,这是一个WordPress模板文件。

下面是代码,包括表单:
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="POST" autocomplete="on" id="register-form">
<input type="text" name="name" placeholder="Name"/>
<input type="email" name="email" placeholder="Email address"/>
<button type="submit" name="submit" class="button">Send
<img src="<?= get_image('icon-arrow-right-tiny.svg'); ?>"/></button>
</form>
<?php
if (isset($_POST['submit'])) {
// validate the email address first
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
// process the form only if the email is valid
if ($email) {
    $to = 'info@example.com'; // where you want to send the mail
    $from = 'info@mydomain.com';
    $subject = 'Website Submission';
    $message = 'Name: ' . $_POST['name'] . "'r'n'r'n";
    $message .= 'Email Address: ' . $_POST['email'] . "'r'n'r'n";
    $headers = "From: $from'r'nReply-to: $email";
    $sent = mail($to, $subject, $message, $headers);
} ?>
<p style='color: #fff; font-size: 14px;'>Thank you for registering your interest.</p>
<?php
}
?>

目前,表单确实被发送了,并且页面确实在表单下面回显"感谢您注册您的兴趣",但是当您单击提交按钮时,它似乎并没有将我们返回到正确的页面。

任何想法?

感谢您的贡献。我已经算出了这个问题,并将在这里分享给任何来这里寻找答案的人。

WordPress为"name"参数保留了一些重要的东西,因此你不能在基于php的表单中使用它。将参数名称从"name"更改为其他名称可以解决此问题。

此外,WordPress还保留了以下名称,您不能在表单中使用它们-"日","月"answers"年"。

我检查了你的代码,我认为你使用了颜色代码#fff,即消息。请尝试使黑色或任何其他颜色的代码其余的工作。:)

感谢您的关注

你必须在你的感谢信下面放一个php代码。

头(地点:$ _SERVER [" PHP_SELF "]);退出;