PHP 联系表 - 如果允许,还有多少?

PHP Contact Form - How many else if are you allowed?

我确定有人问过这个问题,请指出正确的方向,因为我似乎找不到它,谢谢。

正如标题所说,您还可以使用多少个 if 语句?如果有人在名称和主题字段中使用除字母以外的任何内容,我有这个联系表格会吐出一个错误。

当我删除我的主题字段的 preg_match 以便他们可以输入他们想要的任何内容时它工作正常但是当我将它添加回去时即使他们只使用字母它仍然说 'Subject field must only contain letters',我有一种感觉,这是因为你只允许一个 else if 语句。如果是这种情况,我将如何着手显示主题字段的 error_subject 消息和名称字段的 error_name 消息?

非常感谢您提供的任何帮助 :) 谢谢

PHP 联系表格代码:

    <?php

  //Response Generation Function
  $response = "";

  //Function To Generate Response
  function my_contact_form_generate_response($type, $message){

    global $response;

    if($type == "success") $response = "
      <div class='success-message'>
        <div class='success'>{$message}</div>
      </div>";
    else $response = "
      <div class='error-message'>
        <div class='error'>{$message}</div>
      </div>";

  }

  //Response Message
  $not_human       = "Human verification incorrect.";
  $missing_content = "Please fill in all required fields.";
  $error_name = "Full Name field must only contain letters";
  $error_subject = "Subject field must only contain letters";
  $email_invalid   = "E-Mail Address Invalid.";
  $message_unsent  = "Message was not sent. Try Again.";
  $message_sent    = "Thanks! Your message has been sent.";

  //User Posted Variable
  $name = $_POST['message_name'];
  $email = $_POST['message_email'];
  $tele = $_POST['message_tele'];
  $subject = $_POST['message_subject'];
  $message = $_POST['message_text'];
  $human = $_POST['message_human'];

  //PHP Mailer Variables
  $to = 'stephen@e-foreknowledge.co.uk';
  $subject = "New Message - ".get_bloginfo('name');
  $headers = "From: $email\n\n";  
  $body = 
  "Name: $name\n\n Subject: $subject\n\n Telephone: $tele\n\n Message: $message\n";

  if(!$human == 0){
    if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    else {

      //Validate Email
      if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        my_contact_form_generate_response("error", $email_invalid);
      else //Email is Valid
      {
        //Validate Presence of Name and Message
        if(empty($name) || empty($tele) || empty($subject) || empty($message)){
          my_contact_form_generate_response("error", $missing_content);
        } 
        else if(!preg_match("/^[a-zA-Z ]*$/", $name)) {
            my_contact_form_generate_response("error", $error_name);
        }
        else if(!preg_match("/^[a-zA-Z ]*$/", $subject)) {
            my_contact_form_generate_response("error", $error_subject);
        }                 
        else {
          $sent = wp_mail($to, $subject, $headers, $body);
          if($sent) my_contact_form_generate_response("success", $message_sent); //Message Sent!
          else my_contact_form_generate_response("error", $message_unsent); //Mssage Wasn't Sent
        }
      }
    }
  }
  else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

?>

这是我的 WordPress 网站,我正在制作一个自定义联系页面,其中包含联系表格。如果您遇到与我相同的问题,答案如下:)

想多少就有多少。任何语言的任何编程结构都没有限制。限制是磁盘 space 和内存使用。

您的问题在这里:

$subject = "New Message - ".get_bloginfo('name');

你实际上在主题中插入了一个连字符 (-)。

所以问题是我在两个不同的地方调用 $subject,这意味着两个不同的东西,它们发生了冲突。我将一个用于 E-Mail 中显示的主题,另一个用于联系页面上的主题字段。固定代码如下:)

<?php

  //Response Generation Function
  $response = "";

  //Function To Generate Response
  function my_contact_form_generate_response($type, $message){

    global $response;

    if($type == "success") $response = "
      <div class='success-message'>
        <div class='success'>{$message}</div>
      </div>";
    else $response = "
      <div class='error-message'>
        <div class='error'>{$message}</div>
      </div>";

  }

  //Response Message
  $not_human       = "Human verification incorrect.";
  $missing_content = "Please fill in all required fields.";
  $error_name = "Full Name field must only contain letters";
  $error_subject = "Subject field must only contain letters";
  $email_invalid   = "E-Mail Address Invalid.";
  $message_unsent  = "Message was not sent. Try Again.";
  $message_sent    = "Thanks! Your message has been sent.";

  //User Posted Variable
  $name = $_POST['message_name'];
  $email = $_POST['message_email'];
  $tele = $_POST['message_tele'];
  $formsubj = $_POST['message_subject'];
  $message = $_POST['message_text'];
  $human = $_POST['message_human'];

  //PHP Mailer Variables
  $to = 'stephen@e-foreknowledge.co.uk';
  $subject = "New Message - ".get_bloginfo('name');
  $headers = "From: $email\n\n";  
  $body = 
  "Name: $name\n\n Subject: $formsubj\n\n Telephone: $tele\n\n Message: $message\n";

  if(!$human == 0){
    if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    else {

      //Validate Email
      if(!filter_var($email, FILTER_VALIDATE_EMAIL))
        my_contact_form_generate_response("error", $email_invalid);
      else //Email is Valid
      {
        //Validate Presence of Name and Message
        if(empty($name) || empty($tele) || empty($formsubj) || empty($message)){
          my_contact_form_generate_response("error", $missing_content);
        } 
        else if(!preg_match("/^[a-zA-Z ]*$/", $name)) {
            my_contact_form_generate_response("error", $error_name);
        }
        else if(!preg_match("/^[a-zA-Z ]*$/", $formsubj)) {
            my_contact_form_generate_response("error", $error_subject);
        }                 
        else {
          $sent = wp_mail($to, $subject, $headers, $body);
          if($sent) my_contact_form_generate_response("success", $message_sent); //Message Sent!
          else my_contact_form_generate_response("error", $message_unsent); //Mssage Wasn't Sent
        }
      }
    }
  }
  else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

?>