联系表格不工作,无法直接访问

时间:2016-06-04 13:52:13

标签: php html forms contact

我觉得好像一切都已正确完成,但在提交表单时,它无法直接访问。这是来自一个没有联系表格的模板网站,所以我在网上找到了一个,但它似乎没有使用它。

这是html

<form action="mail.php" method="post" class="wow fadeInUp" data-wow-delay="0.6s">
                    <div class="col-md-6 col-sm-6">
                          <input type="text" name="name" class="form-control" placeholder="Your Name...">
                    </div>
                    <div class="col-md-6 col-sm-6">
                         <input type="email" name="email" class="form-control" placeholder="Your Email...">
                    </div>
                    <div class="col-md-12 col-sm-12">
                        <textarea rows="6" name="message" class="form-control" placeholder="Your Message" required="">
</textarea>
                    </div>
                    <div class="col-md-offset-4 col-md-8 col-sm-offset-4 col-sm-8">
                        <input type="submit" class="form-control" value="SEND">
                    </div>
                </form>

这是php

<?php
    /**
     * sends mail submitted from the contact form
     */

    /*
        EDIT BELOW
     */
    $to_Email       = "appydevelopers@gmail.com"; //Replace with your email address
    $subject        = 'Appydeveloper Site'; //Subject line for emails
    /*
        EDIT ABOVE
     */


    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')
        die("No direct access.");

    if(!isset($_POST["name"]) || !isset($_POST["email"]) || !isset($_POST["message"])) {
        $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
        die($output);
    }

    //additional validation
    $user_Name        = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
    $user_Email       = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $user_Message     = filter_var($_POST["message"], FILTER_SANITIZE_STRING);

    if(strlen($user_Name)<4) {
        $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
        die($output);
    }
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) {
        $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
        die($output);
    }
    if(strlen($user_Message)<5) {
        $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
        die($output);
    }

    $sentMail = @mail($to_Email, $subject, $user_Message .'  -'.$user_Name, $headers);

    if(!$sentMail) {
        $output = json_encode(array('type'=>'error', 'text' => 'Server error, could not send email. Sorry for the inconvenience.'));
        die($output);
    } else {
        $output = json_encode(array('type'=>'success', 'text' => 'Message successfully sent!'));
        die($output);
    }
?>

1 个答案:

答案 0 :(得分:0)

当你没有通过Ajax调用这个php文件时为什么要在这里检查isset($_SERVER['HTTP_X_REQUESTED_WITH'])这是在你通过Ajax调用时使用的。所以它根本没有设置。

评论此行并检查

//if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')         //die("No direct access.");