0

我正在尝试使用 PHP 处理表单,但我得到的只是一个带有 PHP 文件名的空白页面。经过数小时的搜索和尝试几件事,我来找你。你能审查代码并提供帮助吗?

html表单:

        <form name="contactForm" id="contactForm" method="POST" action="/form.php">           

           <div class="form-field">
          <input name="contactName" type="text" id="contactName" placeholder="Name" minlength="2" required="">
           </div>

           <div class="row">
              <div class="col-six tab-full">
                <div class="form-field">
                  <input name="contactEmail" type="email" id="contactEmail" placeholder="Email" required="">
                </div>                   
            </div>
            <div class="col-six tab-full">              
              <div class="form-field">
                <input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" >
                </div>                   
            </div>
           </div>

           <div class="form-field">
              <textarea name="contactMessage" id="contactMessage" placeholder="message" rows="10" cols="50" required=""></textarea>
          </div> 


           <div class="form-field">
              <button type="submit" class="submitform" name="submit">Submit</button>

           </div>

      </form> <!-- end form -->

.php 文件

    <?php
    if(isset($_POST['submit'])) {

        $email_to = "me@myemail.com";

        function died($error) {
            // your error code can go here
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }


        // validation expected data exists
        if(!isset($_POST['contactName']) ||
            !isset($_POST['contactEmail']) ||
            !isset($_POST['contactSubject']) ||
            !isset($_POST['contactMessage'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }



        $name = $_POST['contactName']; // required
        $email_from = $_POST['contactEmail']; // required
        $subject = $_POST['contactSubject']; // required
        $message = $_POST['contactMessage']; // not 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 .= 'The Email Address you entered does not appear to be valid.<br />';
      }

        $string_exp = "/^[A-Za-z .'-]+$/";

      if(!preg_match($string_exp,$name)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br />';
      }

      if(strlen($message) < 2) {
        $error_message .= 'The Message you entered appears to be invalid.<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 .= "Name: ".clean_string($name)."\n";
        $email_message .= "Subject: ".clean_string($subject)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\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, $subject, $email_message, $headers);  

    }
    ?>
4

0 回答 0