2017-01-03 41 views
0

獲取錯誤時的聯繫方式應提交。得到錯誤當接觸形式提交

未定義指數:HTTP_X_REQUESTED_WITH在C:\ XAMPP \ htdocs中\ dishadwellings \ dishaparkwest \ contact.php第7行 { 「類型」: 「錯誤」, 「文本」: 「對不起請求必須是Ajax的POST」 }

下面是HTML表單代碼:

<form action="contact.php" method="POST"> 
         <input type="text" name="do-input-name" id="do-input-name" placeholder="Name"> 
         <input type="email" name="do-input-email" id="do-input-email" placeholder="Email"> 
         <input type="text" name="do-input-web" id="do-input-web" placeholder="Web"> 

         <textarea name="do-input-message" id="do-input-message" cols="30" rows="10" class="do-input-message" placeholder="Comment"></textarea> 

         <button type="submit" id="do-submit-btn" class="do-btn-round-solid">SEND</button> 
        </form> 

下面是聯繫表格代碼:我不能糾正錯誤。請做有利於解決這一問題

<?php 
if($_POST) 
{ 
    $to_email  = "[email protected]"; //Recipient email, Replace with own email here 

    //check if its an ajax request, exit if not 
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 

     $output = json_encode(array(//create JSON data 
      'type'=>'error', 
      'text' => 'Sorry Request must be Ajax POST' 
     )); 
     die($output); //exit script outputting json data 
    } 

    //Sanitize input data using PHP filter_var(). 
    $name  = filter_var($_POST["name"], FILTER_SANITIZE_STRING); 
    $email  = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL); 
    $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING); 

    //additional php validation 
    if(strlen($name)<4){ // If length is less than 4 it will output JSON error. 
     $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!')); 
     die($output); 
    } 
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //email validation 
     $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!')); 
     die($output); 
    } 
    if(strlen($message)<3){ //check emtpy message 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.')); 
     die($output); 
    } 

    //email body 
    $message_body = $message."\r\n\r\n-".$name."\r\nEmail : ".$email; 

    //proceed with PHP email. 
    $headers = 'From: '.$name.'' . "\r\n" . 
    'Reply-To: '.$email.'' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

    $send_mail = mail($to_email, $subject, $message_body, $headers); 

    if(!$send_mail) 
    { 
     //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens) 
     $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.')); 
     die($output); 
    }else{ 
     $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email')); 
     die($output); 
    } 
} 
?> 
+0

也發表您的代碼.. –

+0

這裏給它做這個事情提交我 – Rupal

+0

職位的代碼Ajax代碼。 – Saranya

回答

0

試試你的,如果條件如下:

//check if its an ajax request, exit if not 
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) { 

    $output = json_encode(array(//create JSON data 
     'type'=>'error', 
     'text' => 'Sorry Request must be Ajax POST' 
    )); 
    die($output); //exit script outputting json data 
} 

接下來的事情是,您所提交通過普通的HTML表單的形式和您要檢查提交數據或發送電子郵件只有在其ajax提交。這不是真的,所以它不起作用。

爲了使您的代碼的工作,無論是通過AJAX提交表單數據,或者如果上面顯示的條件刪除此。

你的郵編

<?php 
if($_POST) 
{ 
    $to_email  = "[email protected]"; //Recipient email, Replace with own email here 


    //Sanitize input data using PHP filter_var(). 
    $name  = filter_var($_POST["name"], FILTER_SANITIZE_STRING); 
    $email  = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL); 
    $message = filter_var($_POST["message"], FILTER_SANITIZE_STRING); 
    $subject = "Test email"; 
    $user_name = "Sharnya"; 

    //additional php validation 
    if(strlen($name)<4){ // If length is less than 4 it will output JSON error. 
     $output = 'Name is too short or empty!'; 
     die($output); 
    } 
    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //email validation 
     $output = 'Please enter a valid email!'; 
     die($output); 
    } 
    if(strlen($message)<3){ //check emtpy message 
     $output = 'Too short message! Please enter something.'; 
     die($output); 
    } 

    //email body 
    $message_body = $message."\r\n\r\n-".$name."\r\nEmail : ".$email; 

    //proceed with PHP email. 
    $headers = 'From: '.$name.'' . "\r\n" . 
    'Reply-To: '.$email.'' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

    $send_mail = mail($to_email, $subject, $message_body, $headers); 

    if(!$send_mail) 
    { 
     //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens) 
     $output = 'Could not send mail! Please check your PHP mail configuration.'; 
     die($output); 
    }else{ 
     $output = 'Hi '.$user_name .' Thank you for your email'; 
     die($output); 
    } 
} 
?> 

HTML表單代碼:

<form action="contact.php" method="POST" enctype="text/plain"> 
         <input type="text" name="name" id="name" placeholder="Name"> 
         <input type="email" name="email" id="email" placeholder="Email"> 
         <input type="text" name="web" id="web" placeholder="Web"> 

         <textarea name="message" id="message" cols="30" rows="10" class="message" placeholder="Comment"></textarea> 

         <button type="submit" id="submit" class="do-btn-round-solid">SEND</button> 
        </form> 

編碼愉快!

+0

評論是不適用於擴展討論;這次談話一直[移動聊天](​​http://chat.stackoverflow.com/rooms/132163/discussion-on-answer-by-rupal-getting-error-when-the-contact-form-b​​e-submitted) 。 –

+0

謝謝@BhargavRao我正在尋找相同的。但我不知道從哪裏可以創建聊天室。你能告訴我我怎麼樣? – Rupal

+0

由於OP尚未賺取的聊天特權(20聲望),你不能創建一個聊天室。只有版主才能爲這些用戶提供寫入權限。 –