我正在尝试在WordPress中创建一个简单的表单,以在单击“提交”按钮后发送电子邮件,但是,当单击“提交”按钮时,页面刷新,并且将我重定向到404页面。无论我指定要重定向到的位置如何,都会发生这种情况,即使我说要使其重定向到同一页面。无论如何,我可以使用邮件功能并避免遇到我遇到的404错误吗?希望能找到任何帮助!谢谢!
<form method="post">
<h1 class="sectionHeader title">Contact</h1>
<div id="inputContainer">
<div class="flexChildInput" id="nameInput">
<h4 class="contactLabels">Name</h4>
<input type="text" name="name" required />
</div>
<div class="flexChildInput" id="emailInput">
<h4 class="contactLabels">Email</h4>
<input type="text" name="email" required />
</div>
</div>
<div id="commentsContainer">
<h4 class="contactLabels">Comments</h4>
<textarea name="comments" id="" cols="30" style="height: 25vh;" required></textarea>
</div>
<input name="action" hidden />
<div id="submitContainer">
<input type="submit" value="Submit" />
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['submit']) && !empty($_POST(['name'])) && !empty($_POST(['email'])) && !empty($_POST(['comments']))) {
$to = $_POST['myemail@email.com'];
$subject = $_POST['name'] + " has sent you a message!";
$message = $_POST['comments'];
$from = $_POST['email'];
$headers = "From:" . $from;
//(mail($to, $subject, $message, $headers);
if (mail($to, $subject, $message, $headers)) {
echo "Mail Sent.";
} else {
echo "failed";
}
}
}
?>
</div>
</form>
答案 0 :(得分:1)
实现它是如此简单。请参考以下示例代码:
<?php
if(isset($_POST['submit'])){
//code to be executed
}else{
//code to be executed
}
?>
<html>
<head></head>
<body>
<form method="post" action="">
<input type="submit" name="submit">
</form>
</body>
</html>
只有单击提交按钮后,PHP脚本才会运行。