PHP 联系表格如果有错误则保留输入字段,否则清除
PHP contact form keep input fields if error, otherwise clear
我希望我的联系表单输入字段在出现错误时保存用户的输入,否则在电子邮件通过时将其清除。现在它可以工作,除非发送电子邮件时字段仍然填写。我可以刷新页面,但它不会显示 'Your email has been sent' 消息。
这是我的表格:
<form class="contact-form" action="" method="post">
<input type="text" name="name" value="<?php if(isset($_POST["name"])) echo $_POST["name"]; ?>" />
</form>
我尝试在处理电子邮件业务的 php 代码中添加一些内容 - 如果消息已发送,unset($_POST["name"])
,并且还添加到此表单输入的 php else echo '';
但这似乎没有用。变量似乎仍已设置。
您应该在发生错误时设置错误标志。试试这个
$错误=假;
如果(isset($_POST['submit'])){
$msg = $_POST['msg'];
if(mail("someone@example.com","My subject",$msg)){
}else{
$error = "mail does not sent";
}
}
" />
假设您的页面是 contact.php
。
您 php 代码应该是这样的:
// we will keep here error message
$error = '';
// if request is get and isset sent
if ($_SERVER["REQUEST_METHOD"] === "GET" and isset($_GET["sent"]))
echo '<p id="output-area">Message has been sent.</p>';
else {
// if request is post
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// then verify input data
if (!empty($_POST['msg'])) {
// if mail was sent, redirect to contact.php?sent
if (mail("someone@example.com", "My subject", $_POST['msg'])){
header('Location: contact.php?sent');
exit;
} else
$error = "Mail does not sent.";
} else
$error = 'Please fill in all inputs.';
}
}
if ($error != '')
echo '<p class="error">Error: ' . $error . '</p>';
// here goes your form
echo '<form class="contact-form" action="contact.php" method="post">
<textarea name="msg">' . (!empty($_POST["msg"]) ? $_POST["msg"] : '') . '</textarea>
<input type="submit" value="send">
</form>';
我希望我的联系表单输入字段在出现错误时保存用户的输入,否则在电子邮件通过时将其清除。现在它可以工作,除非发送电子邮件时字段仍然填写。我可以刷新页面,但它不会显示 'Your email has been sent' 消息。
这是我的表格:
<form class="contact-form" action="" method="post">
<input type="text" name="name" value="<?php if(isset($_POST["name"])) echo $_POST["name"]; ?>" />
</form>
我尝试在处理电子邮件业务的 php 代码中添加一些内容 - 如果消息已发送,unset($_POST["name"])
,并且还添加到此表单输入的 php else echo '';
但这似乎没有用。变量似乎仍已设置。
您应该在发生错误时设置错误标志。试试这个
$错误=假; 如果(isset($_POST['submit'])){
$msg = $_POST['msg'];
if(mail("someone@example.com","My subject",$msg)){
}else{
$error = "mail does not sent";
}
}
" />假设您的页面是 contact.php
。
您 php 代码应该是这样的:
// we will keep here error message
$error = '';
// if request is get and isset sent
if ($_SERVER["REQUEST_METHOD"] === "GET" and isset($_GET["sent"]))
echo '<p id="output-area">Message has been sent.</p>';
else {
// if request is post
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// then verify input data
if (!empty($_POST['msg'])) {
// if mail was sent, redirect to contact.php?sent
if (mail("someone@example.com", "My subject", $_POST['msg'])){
header('Location: contact.php?sent');
exit;
} else
$error = "Mail does not sent.";
} else
$error = 'Please fill in all inputs.';
}
}
if ($error != '')
echo '<p class="error">Error: ' . $error . '</p>';
// here goes your form
echo '<form class="contact-form" action="contact.php" method="post">
<textarea name="msg">' . (!empty($_POST["msg"]) ? $_POST["msg"] : '') . '</textarea>
<input type="submit" value="send">
</form>';