我有一个简单的一页联系表单,具有以下逻辑
这是完整的代码。我正在使用$_POST = array();
尝试清除这些值,但是似乎没有用。
<form id="contact_form" method="POST">
Name<br/><input type="text" name="name" value="<?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?>" required><br/>
Email<br/><input type="text" name="email" value="<?php echo isset($_POST["email"]) ? $_POST["email"] : ''; ?>" required><br/>
...
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
...
if (mail($recipient, $subject, $message, $headers) == TRUE) {
$_POST = array();
echo "Your msg has been sent.";
}
else {
echo "An error occurred while trying to send your message.";
}
}
?>
任何帮助将不胜感激。
答案 0 :(得分:2)
实际上,您正在使用过帐数据,然后再检查其是否正确,因此它将每次显示该数据。您可以将php代码放在表单之前,如果成功则设置一个标志,然后在输入值属性中不打印任何内容。
<?php
$success = false;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['name'];
$email = $_POST['email'];
...
if (mail($recipient, $subject, $message, $headers) == TRUE) {
$success = true;
echo "Your msg has been sent.";
}
else {
echo "An error occurred while trying to send your message.";
}
}
?>
<form id="contact_form" method="POST">
Name<br/><input type="text" name="name" value="<?php echo (!$success) ? $_POST["name"] : ""; ?>" required><br/>
Email<br/><input type="text" name="email" value="<?php echo isset($_POST["email"]) && (!$success) ? $_POST["email"] : ''; ?>" required><br/>
...
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>