2017-05-09 83 views
0

使用此表單模板創建表單,並取出幾個字段,因爲它們並非都是必需的。PHP表單不會重定向到標題 - 不會重定向

http://www.freecontactform.com/html_form.php

然而,我的頭命令不被重定向我的網頁。這是代碼。請注意,我已經試過,最後沒有退出。

問題不在於它不會發送電子郵件,而是不會重定向。

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

     // CHANGE THE TWO LINES BELOW 
     $email_to = "[email protected]"; 

     $email_subject = "Email from email.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['first_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

    $first_name = $_POST['first_name']; // required 
    $email_from = $_POST['email']; // required 
    $comments = $_POST['comments']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
     if(!preg_match($email_exp,$email_from)) { 
     $error_comments .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 
      $string_exp = "/^[A-Za-z .'-]+$/"; 
      if(!preg_match($string_exp,$first_name)) { 
      $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 
     if(strlen($comments) < 2) { 
     $error_message .= 'The Comments you entered do not appear to be valid.<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($first_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\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); 

header("Location: http://www.website.com/contactSoon.html"); 
} 

?> 

這裏的聯繫方式,我using-

<form name="htmlform" method="post" action="html_form_send.php"> 
         <div class="row form-group"> 
           <div class="col-md-12"> 
           <label for="first_name">Name *</label> 
           <input type="text" name="first_name" class="form-control"> 
           </div> 
           </div> 
         <div class="row form-group"> 
           <div class="col-md-12"> 
           <label for="comments">Email Address *</label> 
           <input type="text" id="email" class="form-control"> 
           </div> 
         </div> 
         <div class="row form-group"> 
           <div class="col-md-12"> 
         <label for="message">Message *</label> 
         <textarea name="message" id="message" cols="30" rows="10" class="form-control"></textarea> 
           </div> 
          </div> 
          <div class="g-recaptcha" data-sitekey="6LfmXB4UAAAAAHxguDJIQXClAG_8rGe6qnK1SldM"></div> 
          <div class="form-group"> 
           <input type="submit" name="submit" id="submit" value="Send Message" class="btn btn-primary"> 
          </div> 

         </form> 
+2

打開。在調用'header()'之前,你幾乎肯定會發送頭文件。 –

+2

您的電子郵件正則表達式不正確,並且對於某些有效的電子郵件將失敗。而是在'FILTER_VALIDATE_EMAIL'中使用'filter_var()'。 –

+0

嘗試在文件的第一行添加ob_start(),該文件就在您打開的PHP標記旁邊,如<?php ob_start();另外,總是添加退出;只是在標題重定向的下一行 – manian

回答

0

更改爲

<?php 
//memory increase max 
ini_set('memory_limit', '-1'); 
if(isset($_POST['email'])) { 

      // CHANGE THE TWO LINES BELOW 
      $email_to = "[email protected]"; 

      $email_subject = "Email from email.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['first_name']) || 
      !isset($_POST['email']) || 
      !isset($_POST['comments'])) { 
      died('We are sorry, but there appears to be a problem with the form you submitted.');  
     } 

     $first_name = $_POST['first_name']; // required 
     $email_from = $_POST['email']; // required 
     $comments = $_POST['comments']; // required 

     $error_message = ""; 
     $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 
      if(!preg_match($email_exp,$email_from)) { 
      $error_comments .= 'The Email Address you entered does not appear to be valid.<br />'; 
     } 
       $string_exp = "/^[A-Za-z .'-]+$/"; 
       if(!preg_match($string_exp,$first_name)) { 
       $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
     } 
      if(strlen($comments) < 2) { 
      $error_message .= 'The Comments you entered do not appear to be valid.<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($first_name)."\n"; 
     $email_message .= "Email: ".clean_string($email_from)."\n"; 
     $email_message .= "Comments: ".clean_string($comments)."\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); 
    echo '<script>window.location.href = "http://www.website.com/contactSoon.html";</script>'; 
    //header("Location: http://www.website.com/contactSoon.html"); 
    } 

    ?> 

所以它會使用JavaScript重定向insted的PHP的錯誤報告

+0

謝謝你的建議。提交表單後,我仍然空白。 –

+0

這是您的php內存問題我將更新答案 –

+0

更新請確保在支持php郵件功能電子郵件的服務器上測試它,因爲如果您在本地運行它,它將不會發送電子郵件,它會創建一個.txt文件放在名爲mailoutput的文件夾中。 –