【问题标题】:Getting error when the contact form be submitted提交联系表格时出现错误
【发布时间】:2017-05-17 03:05:33
【问题描述】:

应提交联系表单时出错。

未定义索引:第 7 行 C:\xampp\htdocs\dishadwellings\dishaparkwest\contact.php 中的 HTTP_X_REQUESTED_WITH {"type":"error","text":"抱歉请求必须是 Ajax POST"}

这是 HTML FORM 代码:

<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       = "abc@gmail.com"; //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);
    }
}
?>

【问题讨论】:

  • 也发布你的代码..
  • 在此处给出执行此提交操作的 ajax 代码
  • 你能echo $_SERVER['HTTP_X_REQUESTED_WITH']."&lt;br&gt; requested with ".strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])
  • 当我做 echo 时遇到同样的错误

标签: php contact-form


【解决方案1】:

试试你的 if 条件如下:

//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 提交您的表单数据或删除此 if 条件如上所示。

您的邮政编码

<?php
if($_POST)
{
    $to_email       = "abc@gmail.com"; //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> 

编码愉快!

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 谢谢@BhargavRao 我正在寻找相同的东西。但我不知道从哪里可以创建聊天室。你能告诉我怎么做吗?
  • 由于OP尚未获得聊天权限(20声望),您无法创建聊天室。只有版主才能为这些用户提供写入权限。
猜你喜欢
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
相关资源
最近更新 更多