联系表单不会以HTML格式显示确认消息

时间:2014-09-09 07:39:51

标签: php json contact-form

我下载了一个很棒的网站模板,除联系表单外,一切正常。没有更多的模板支持,所以现在我在这里。

表单会向我发送包含所有数据的电子邮件,但邮件无法正常运行。

  1. 当数据填写错误时,您会收到错误填写的消息。但是当表单填写正确时,不会删除该消息。
  2. 发送电子邮件时,没有显示已发送的消息。
  3. 发送电子邮件时,表单不会重置。
  4. 我找不到溶剂......

    页面上的HTML。     

            <form id="contact-form" class="contact-form" action="#">
                <p class="contact-name">
                    <input id="contact_name" type="text" placeholder="Volledige naam" value="" name="name" />
                </p>
                <p class="contact-email">
                    <input id="contact_email" type="text" placeholder="E-mail adres" value="" name="email" />
                </p>
                <p class="contact-list">
                <select id="contact_list" size="1" name="reason">
                        <option value="1" selected="selected">Kies de reden voor contact</option>
                        <option value="Informatie aanvraag">Informatie aanvraag</option>
                        <option value="Offerte aanvraag">Offerte aanvraag</option>
                        <option value="Anders">Andere reden</option>
                    </select>
                </p>
                <p class="contact-message">
                    <textarea id="contact_message" placeholder="Uw bericht" name="message" rows="15" cols="40"></textarea>
                </p>
                <p class="contact-submit">
                    <a id="contact-submit" class="submit" href="#">Verstuur uw e-mail</a>
                </p>
    
                <div id="response">
    
                </div>
            </form>
    
        </div>
    

    PHP文件(contact.php)

    <?php
    /*
    * Contact Form Class
    */
    
    
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    
    $admin_email = 'info@fake.nl'; // 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->reason = trim($details['reason']);
            $this->subject = 'Contact via de website'; // 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>Voer een naam in</p>';
                $this->response_status = 0;
            }
    
            // Check email
            if(!$this->email)
            {
                $this->response_html .= '<p>Voer een e-mail adres in</p>';
                $this->response_status = 0;
            }
    
            // Check valid email
            if($this->email && !$this->validateEmail())
            {
                $this->response_html .= '<p>Voer een geldig e-mail adres in</p>';
                $this->response_status = 0;
            }
    
            // Check valid choice
            if($this->reason == '1')
            {
                $this->response_html .= '<p>Maak een geldige keuze</p>';
                $this->response_status = 0;
            }
    
            // Check message length
            if(!$this->message || strlen($this->message) < $this->message_min_length)
            {
                $this->response_html .= '<p>Voer een bericht in. Het zou minstens '.$this->message_min_length.' tekens moeten hebben.</p>';
                $this->response_status = 0;
            }
        }
    
    
        private function sendEmail(){
            $mail = mail($this->email_admin, $this->subject, $this->reason, $this->message,
                 "From: ".$this->name." <".$this->email.">\r\n"
                ."Reply-To: ".$this->email."\r\n"
            ."X-Mailer: PHP/" . phpversion());
    
            if($mail)
            {
                $this->response_status = 1;
                $this->response_html = '<p>Bedankt voor uw bericht.</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();
    
    ?>
    

2 个答案:

答案 0 :(得分:0)

解决了邮件问题。

我删除了表单中的Reason选项字段和PHP中的原因类。 不知道为什么它不适用于原因类。

也许有人可以回答这个问题?

答案 1 :(得分:0)

尝试:

        <select id="contact_list" size="1" name="reason">
                <option value="1" selected="selected">Kies de reden voor contact</option>
                <option value="2">Informatie aanvraag</option>
                <option value="3">Offerte aanvraag</option>
                <option value="4">Andere reden</option>
            </select>