我试图在提交后清理表单。我不确定我错过了什么。我将附上下面的PHP代码。这是我第一次使用PHP编写联系表单或任何内容,所以请耐心等待。提前谢谢你们!
很抱歉以为我附上了代码。
<?php
if(isset($_POST['email'])) {
// EMAIL AND SUBJECT
$email_to = "##@##.com";
$email_subject = "New form submission";
function died($error) {
// your error code can go here
echo "<span style='color: red;' /><center> We're sorry, but there's errors found with the form you submitted.<br /><br /></center></span>";
echo $error."<br /><br />";
echo '<span style="color: red;" /><center>Please go back and fix these errors.<br /><br /></center></span>';
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.</center></span>');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= '<span style="color: red;" /><center>The Email Address you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[0-9.-]+$/";
if(!preg_match($string_exp,$telephone) > 10) {
$error_message .= '<span style="color: red;" /><center>The Telephone number you entered does not appear to be valid.<br /></center></span>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= '<span style="color: red;" /><center>The First Name you entered does not appear to be valid.<br /></center></span>';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= '<span style="color: red;" /><center>The Last Name you entered does not appear to be valid.<br /></center></span>';
}
if(strlen($message) < 10) {
$error_message .= '<span style="color: red;" /><center>The Message you typed seems to be too short, add more words, please.</center></span><br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- The success html below -->
<span style="color: green;"><center>Thank you for contacting us. We will be in touch with you very soon.</center></span>
<?php
}
die();
?>
答案 0 :(得分:2)
您需要做的就是像最初一样重新加载表单。我通常会创建一个显示表单的函数。当脚本的第一部分检测到表单是否已提交时。如果不是,则显示表单。如果是,则处理输入然后显示表单。
这是一些帮助的伪代码。
<?php
function display_form () {
// display your form here
// For my sample, I assume a clear button and a submit button
}
if (isset($_POST['submit'])) { // Submit button was clicked
// put your form processing code here
} else { // Form was not submitted or 'clear' was clicked
display_form();
}
?>
答案 1 :(得分:1)
您可以拥有一个包含表单和所有信息的页面,例如contact.php,但可以通过表单将数据提交到另一个页面,例如,
然后在process_page.php上,处理完数据后,将用户重定向回原始页面并附加查询字符串,例如
header('Location: contact.php?status=mail%20sent%20successfully');
并通过echo $_GET['status']
显示查询字符串,或根据该状态创建备用消息/布局。不要忘记处理设置了$ _GET的情况,但也是空的!
这有点好,因为它将处理逻辑与网站的交互部分分开。