表格表明它已发送,但我什么也没收到

时间:2018-08-22 19:45:03

标签: javascript php html forms

我的表格存在一个我似乎无法解决的问题,希望这里有人可以提供帮助。请在下面查看代码。

这是检查并发送代码,我缺少一些东西

<script type="text/javascript">

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'myemail@gmail.com'; // Your Email
$message_min_length = 5; // Min Message Length


class Contact_Form{
    function __construct($details, $email_admin, $message_min_length){

        $this->name = stripslashes($details['name']);
        $this->email = trim($details['email']);
        $this->subject = 'Massage'; // Subject 
        $this->message = stripslashes($details['message']);

        $this->email_admin = $email_admin;
        $this->message_min_length = $message_min_length;

        $this->response_status = 1;
        $this->response_html = '';
    }


    private function validateEmail(){
        $regex = '/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i';

        if($this->email == '') { 
            return false;
        } else {
            $string = preg_replace($regex, '', $this->email);
        }

        return empty($string) ? true : false;
    }


    private function validateFields(){
        // Check name
        if(!$this->name)
        {
            $this->response_html .= '<p>name please</p>';
            $this->response_status = 0;
        }

        // Check email
        if(!$this->email)
        {
            $this->response_html .= '<p>email please</p>';
            $this->response_status = 0;
        }

        // Check valid email
        if($this->email && !$this->validateEmail())
        {
            $this->response_html .= '<p>email not right</p>';
            $this->response_status = 0;
        }

        // Check message length
        if(!$this->message || strlen($this->message) < $this->message_min_length)
        {
            $this->response_html .= '<p>To short please more then '.$this->message_min_length.' characters</p>';
            $this->response_status = 0;
        }
    }


    private function sendEmail(){
        $mail = mail($this->email_admin, $this->subject, $this->message,
             "Van: ".$this->name." <".$this->email.">\r\n"
            ."Reageer op: ".$this->email."\r\n"
        ."X-Mailer: PHP/" . HTMLversion());

        if($mail)
        {
            $this->response_status = 1;
            $this->response_html = '<p>E-mail send</p>';
        }
    }


    function sendRequest(){
        $this->validateFields();
        if($this->response_status)
        {
            $this->sendEmail();
        }

        $response = array();
        $response['status'] = $this->response_status;   
        $response['html'] = $this->response_html;

        echo json_encode($response);
    }
}
$contact_form = new Contact_Form($_POST, $admin_email, 
$message_min_length);
$contact_form->sendRequest();

 </script>

这是我用来检查和发送电子邮件的脚本。

<form id="contact-form" class="contact-form" action="/contact.html" target="_self" method="post"
    <p class="contact-name">
        <input id="contact_name" type="text" placeholder="Full Name" value="" name="name" />
    </p>
    <p class="contact-email">
        <input id="contact_email" type="text" placeholder="Email Address" value="" name="email" />
    </p>
    <p class="contact-message">
        <textarea id="contact_message" placeholder="Your Message" name="message" rows="15" cols="40"></textarea>
    </p>
    <p class="contact-submit">
        <a style="color: white;" id="contact-submit" class="submit" value="submit">Send Your Email</a>
    </p>

    <div id="response">

    </div>
</form>

一切似乎都正常,但是电子邮件没有收到,甚至没有发送。

3 个答案:

答案 0 :(得分:2)

看起来您的php代码位于<script>标记中,但是您应该改用<?php [your script] ?>

此外,表单的开始标签缺少>

答案 1 :(得分:1)

您表单上的操作是一个HTML文件,将不会执行任何操作。

您需要将表单处理脚本作为php文件(假设您的服务器已经能够处理php),并且对表单的操作应类似于

<form id="contact-form" class="contact-form" action="/contact.php" target="_self" method="post">

请注意,由于

标记仍处于打开状态,因此我还在帖子后添加了结束符“>”。

答案 2 :(得分:0)

好像您在HTML脚本标记中使用PHP,且其type属性设置为“ text / javascript”

尝试

<script language=php>
    ...Your code here...
</script>

或者带有基本的php标签