2015-10-25 52 views
0

我一直在使用這個php代碼作爲我的表單驗證的一部分。它在過去6個月裏一直工作正常。突然我提交表單每次我不斷收到以下自定義錯誤消息我已經安裝在這裏:爲什麼我的表單會拋出錯誤?

header('HTTP/1.1 500 Our server is under maintenance!<br> If this error persists please contact us directly: <strong>[email protected]</strong>'); 

我測試過的表單字段中的PHP驗證這似乎工作確定。 我沒有改變任何東西在我的PHP或AJAX邏輯或HTML格式本身。無法弄清楚它有什麼問題。

這裏是我使用PHP:

<?php 
if($_POST) 
{ 
    //check if its an ajax request, exit if not 
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 
     die(); 
    } 


    //check $_POST vars are set, exit if any missing 
    if(!isset($_POST["userName"]) || !isset($_POST["userLastName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userArrival"]) || !isset($_POST["userNumberPeople"]) 
    || !isset($_POST["userPickupLocation"]) || !isset($_POST["userRequest"]) || !isset($_POST["userTourSelection"])) 
    { 
     die(); 
    } 

    //Sanitize input data using PHP filter_var(). 
    $user_Name    = filter_var($_POST["userName"], FILTER_SANITIZE_STRING); 
    $user_LastName   = filter_var($_POST["userLastName"], FILTER_SANITIZE_STRING); 
    $user_Email    = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL); 
    $user_TourSelection  = filter_var($_POST["userTourSelection"], FILTER_SANITIZE_STRING); 
    $user_Arrival   = filter_var($_POST["userArrival"], FILTER_SANITIZE_STRING); 
    $user_NumberPeople  = filter_var($_POST["userNumberPeople"], FILTER_SANITIZE_STRING); 
    $user_PickupLocation = filter_var($_POST["userPickupLocation"], FILTER_SANITIZE_STRING); 
    $user_Request   = filter_var($_POST["userRequest"], FILTER_SANITIZE_STRING); 



    $to_Email  = "[email protected]"; //Replace with recipient email address 
    $subject  = 'Tour request: '.$user_TourSelection.' '; //Subject line for emails 


    //additional php validation 
    if(strlen($user_Name)<2) // If length is less than 4 it will throw an HTTP error. 
    { 
     header('HTTP/1.1 500 Name is too short or empty!'); 
     exit(); 
    } 
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation 
    { 
     header('HTTP/1.1 500 Please enter a valid email address!'); 
     exit(); 
    } 
    if(strlen($user_Request)<5) //check emtpy message 
    { 
     header('HTTP/1.1 500 Please explain in a few words how you would like us to assist you.'); 
     exit(); 
    } 


    // message 
$message = '<strong>Name:</strong> '.$user_Name.' '.$user_LastName.' <br><br> 
      <strong>Date of Arrival:</strong> '.$user_Arrival.' <br><br> 
      <strong>Tour:</strong> '.$user_TourSelection.' <br><br> 
      <strong>Number of people:</strong> '.$user_NumberPeople.' <br><br> 
      <strong>Pick-Up Location:</strong> '.$user_PickupLocation.' <br><br> 
      <strong>Request:</strong> '.$user_Request.' 
      '; 


//proceed with PHP email. 
    $headers = 'From: '.$user_Email.'' . "\r\n" . 
    $headers .='Reply-To: '.$user_Email.'' . "\r\n" ; 
    $headers .= 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $headers .='X-Mailer: PHP/' . phpversion(); 

    @$sentMail = mail($to_Email, $subject, $message, $headers); 

    if(!$sentMail) 
    { 
     header('HTTP/1.1 500 Our server is under maintenance!<br> If this error persists please contact us directly: <strong>[email protected]</strong>'); 
     exit(); 
    }else{ 
     echo 'Congratulations '.$user_Name .'! '; 
     echo 'We have received your request and we will respond as soon as possible.'; 
    } 
} 
?> 

這裏是JS

$(document).ready(function() { 

    $("#submit_btn").click(function() { 
     //collect input field values 
     var user_name  = $('input[name=firstName]').val(); 
     var user_lastname = $('input[name=lastName]').val(); 
     var user_email  = $('input[name=email]').val(); 
     var user_numberpeople = $('select#numberofPeople').val(); 
     var user_pickuplocation = $('select#pickupLocation').val(); 
     var user_arrivaldate = $('input[name=arrivalDate]').val(); 
     var user_request = $('textarea[name=request]').val(); 
     var user_tourselection = $('#tourSelection option:selected').val(); 

     //simple validation at client's end 
     //we simply change border color to red if empty field using .css() 
     var proceed = true; 

     if(user_name==""){ 
      $('input[name=firstName]').addClass("required"); 
      proceed = false; 
     } 
     if(user_lastname==""){ 
      $('input[name=lastName]').addClass("required"); 
      proceed = false; 
     } 
     if(user_email=="") {  
      $('input[name=email]').addClass("required"); 
      proceed = false; 
     } 
     if(user_numberpeople=="") {  
      $('select#numberofPeople').addClass("required"); 
      proceed = false; 
     } 

     if(user_pickuplocation=="") {  
      $('select#pickupLocation').addClass("required"); 
      proceed = false; 
     } 


     if(user_arrivaldate=="") {  
      $('input[name=arrivalDate]').addClass("required"); 
      proceed = false; 
     } 

     if(user_request=="") { 
      $('textarea[name=request]').addClass("required"); 
      proceed = false; 
     } 

     if(user_tourselection=="") { 
      $('select#tourSelection').addClass("required"); 
      proceed = false; 
     } 



     //everything looks good! proceed... 
     if(proceed){ 

      //deactivate button 
      $("#reservation-form button#submit_btn").attr("disabled", "disabled").text('Processing...').addClass("processing-state"); 

      //data to be sent to server 
      var post_data = { 

       'userName':user_name, 
       'userLastName':user_lastname, 
       'userEmail':user_email, 
       'userNumberPeople':user_numberpeople, 
       'userPickupLocation':user_pickuplocation, 
       'userArrival':user_arrivaldate, 
       'userRequest':user_request, 
       'userTourSelection':user_tourselection 
      }; 

      //Ajax post data to server 
      $.post('http://www.myurl.com/reservation.php', post_data, function(data){ 

       //load success message in #result div element, with slide effect. 
       $("#result").hide().html('<span class="success">'+data+'</span>').slideDown(); 

       $('label').hide(); 
       $('select').hide(); 
       $('#reservation-form h2').hide(); 
       ga('send', 'event', 'tours', 'request-tour', 'Tour-Request-Complete'); 


      }).fail(function(err) { //load any error data 

       $("#reservation-form button#submit_btn").text('REQUEST FAILED...').removeClass("processing-state").addClass("fail-state"); 
       $("#result").hide().html('<div class="error">'+err.statusText+'</div>').slideDown(); 

       ga('send', 'event', 'tours', 'request-tour', 'Tour-Request-Error'); 

       $("#reservation-form input, #reservation-form textarea").focus(function() { 

        $("#reservation-form button#submit_btn").removeAttr("disabled").text('SEND YOUR REQUEST').removeClass("fail-state"); 
        $("#result").slideUp(); 

       }); 

      }); 
     } 
    }); 


    //reset previously set border colors and hide all message on .keyup() 
    $("input, textarea").keyup(function() { 
     $(this).removeClass("required"); 
    }); 


    $("select#tourSelection").change(function() { 

     if($("select#tourSelection").val() != '') 

      {$("select#tourSelection").removeClass("required"); 
      } 

    }); 


     $("select#numberofPeople").change(function() { 

     if($("select#numberofPeople").val() != '') 

      {$("select#numberofPeople").removeClass("required"); 
      } 

    }); 

     $("select#pickupLocation").change(function() { 

     if($("select#pickupLocation").val() != '') 

      {$("select#pickupLocation").removeClass("required"); 
      } 

    }); 




}); 

和HTML

<fieldset id="reservation-form"> 

<div id="result"></div> 
    <h2>Please fill out the form</h2> 

    <label for="firstName"> 
    <input type="text" name="firstName" id="firstName" placeholder="FIRST NAME" /> 
    </label> 

    <label for="lastName"> 
    <input type="text" name="lastName" id="lastName" placeholder="LAST NAME" /> 
    </label> 

    <label for="email"> 
    <input type="text" name="email" id="email" placeholder="EMAIL" /> 
    </label> 

    <select id="tourSelection"> 
    <option value="">PLEASE SELECT YOUR TOUR</option> 
    <option value="ATHENS TOUR">ATHENS TOUR</option> 
    <option value="ATHENS and SOUNION TOUR">ATHENS &amp; SOUNION TOUR</option> 
    <option value="ATHENS and CORINTH TOUR">ATHENS &amp; CORINTH TOUR</option> 
    <option value="PELOPONNESE TOUR">PELOPONNESE TOUR</option> 
    <option value="DELPHI TOUR">DELPHI TOUR</option> 
    <option value="DELPHI and THERMOPYLAE TOUR">DELPHI &amp; THERMOPYLAE TOUR</option> 
    <option value="VRAVRONA MARATHON and SOUNION TOUR">VRAVRONA MARATHON &amp; SOUNION</option> 
    <option value="ATHENS HALF DAY TOUR">ATHENS HALF DAY TOUR</option> 
    <option value="CORINTH TOUR">CORINTH TOUR</option> 
    <option value="SOUNION TOUR">SOUNION TOUR</option> 
    <option value="SOUNION SUNSET TOUR">SOUNION SUNSET TOUR</option> 
    <option value="NAFPLIO TOUR">NAFPLIO TOUR</option> 
    <option value="DELPHI and METEORA TOUR">DELPHI &amp; METEORA TOUR</option> 
    <option value="ARGOLIDA and ANCIENT OLYMPIA TOUR">ARGOLIDA &amp; ANCIENT OLYMPIA TOUR</option> 
    <option value="THREE DAY PELOPONNESE TOUR">THREE DAY PELOPONNESE TOUR</option> 
    <option value="FOUR DAY PELOPONNESE TOUR">FOUR DAY PELOPONNESE TOUR</option> 
    <option value="CUSTOM TOUR">CUSTOM TOUR</option> 
    </select> 


    <select id="numberofPeople"> 
     <option value="">TOTAL NUMBER OF PEOPLE IN YOUR PARTY</option> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
     <option value="4">4</option> 
     <option value="5">5</option> 
     <option value="6">6</option> 
     <option value="7">7</option> 
     <option value="8">8</option> 
     <option value="Over 8">Over 8</option> 
    </select> 


     <select id="pickupLocation"> 
     <option value="">SPECIFY PICK-UP LOCATION</option> 
     <option value="Port">PORT</option> 
     <option value="Airport">AIRPORT</option> 
     <option value="Hotel">HOTEL</option> 
     <option value="Other">OTHER</option> 
     </select> 




    <label for="arrivalDate"> 
    <input type="text" name="arrivalDate" id="arrivalDate" placeholder="DATE YOU WISH TO DO THE TOUR" /> 
    </label> 

    <label for="request"> 
    <textarea name="request" id="request" placeholder="YOUR PERSONAL REQUEST"></textarea> 
    </label> 

    <label> 
    <button class="submit_btn" id="submit_btn">SEND YOUR REQUEST</button> 
    </label> 
</fieldset> 

的問題是,形式不發送數據到指定的電子郵件。它總是顯示自定義錯誤消息。

任何幫助,非常感謝。

謝謝!


PHP錯誤報告顯示以下內容:

Array ([type] => 8192 [message] => Directive 'register_globals' is deprecated in PHP 5.3 and greater [file] => Unknown [line] => 0) 

更新 加入php_flag register_globals off我htaccess的我不再讓PHP的錯誤消息,但它仍然沒有提交後。保持從第72行(php代碼)加載自定義錯誤消息。

+0

你沒有解釋錯誤。 – fico7489

+0

@ fico7489表單不提交。 –

+0

首先刪除'@'錯誤抑制器,使用錯誤報告併發布您的HTML表單和相關的Ajax。 –

回答

1

該問題是由電子郵件發送引起的。 檢查郵件功能是否正常。問題可能出現在服務器配置中。

+0

是我需要檢查與我的託管服務提供商? –

+0

只需用簡單的代碼創建新的php文件: '<?php var_dump(mail('your_email','email test','email test - OK!')); print_r(error_get_last()); ?>' – Neodan

+0

Array([type] => 8 [message] =>未定義變量:headers [file] => /services16/webpages/util/a/t/mydomain.site.myprovider.com/public/reservation。 php [line] => 63) 這是什麼意思? (reservation.php是我上面的php代碼) –

2

郵件無法在該行

@$sentMail = mail($to_Email, $subject, $message, $headers); 

刪除「@」字符發送,它可能會給你爲什麼沒有發送的消息。

可能的問題可能是您不允許從服務器發送郵件或PHP配置中的某些內容已更改!

編輯:要捕獲錯誤消息,您應該使用error_get_last函數。嘗試在第72行添加print_r(error_get_last()); exit;.

+0

刪除'@'字符,但沒有改變任何東西。 –

+0

要捕獲錯誤消息,您應該使用'error_get_last'函數。嘗試添加'print_r(error_get_last());出口; 'if'(!$ sentMail){'後。這應該會給你更多的信息。問題可能是您的服務提供商對send_mail功能或PHP配置所做的更改。 –

+0

檢查服務提供商。配置中沒有任何更改。 –

相關問題