2017-01-03 42 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声望),你不能创建一个聊天室。只有版主才能为这些用户提供写入权限。 –