0

我正在尝试在提交后清理表格。我不确定我错过了什么。我将附上下面的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();
?>
4

2 回答 2

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();

  }
?>
于 2013-08-04T03:53:59.017 回答
1

您可以有一个包含表单和所有信息的页面,例如contact.php,但通过表单将数据提交到另一个页面,例如,

然后在 process_page.php 上,处理完数据后,将用户重定向回带有附加查询字符串的原始页面,例如

header('Location: contact.php?status=mail%20sent%20successfully');

并通过显示查询字符串echo $_GET['status']或根据该状态创建替代消息/布局。不要忘记处理设置了 $_GET 但也为空的情况!

这更好一点,因为它将您的处理逻辑与网站的交互部分分开。

于 2013-08-07T14:53:05.927 回答