为什么当我点击我的网页发送时,PHP代码显示而不是发送电子邮件

时间:2015-02-27 03:53:21

标签: php html iis

我无法弄清楚为什么当我点击发送它只是在网页上显示php。我做了我能想到的一切,让它发挥作用。我是PHP的新手,需要让它工作。

PHP

<?php
/*
* Contact Form Class
*/


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

$admin_email = 'email@email.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 = 'Contact from Your 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>Please enter your name</p>';
        $this->response_status = 0;
    }

    // Check email
    if(!$this->email)
    {
        $this->response_html .= '<p>Please enter an e-mail address</p>';
        $this->response_status = 0;
    }

    // Check valid email
    if($this->email && !$this->validateEmail())
    {
        $this->response_html .= '<p>Please enter a valid e-mail address</p>';
        $this->response_status = 0;
    }

    // Check message length
    if(!$this->message || strlen($this->message) < $this->message_min_length)
    {
        $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
        $this->response_status = 0;
    }
}


private function sendEmail(){
    $mail = mail($this->email_admin, $this->subject, $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>Thank You!</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();

?>

HTML

<div class="row">
    <div class="span9">

        <form id="contact-form" class="contact-form" action="_include/php/contact.php" 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="Description of your issue" name="message" rows="15" cols="40"></textarea>
            </p>
            <p class="contact-submit">
                <a id="contact-submit" class="submit" onclick="document.getElementById('contact-form').submit();" href="#">Send Your Email</a>
                <!--<a id="contact-submit" class="submit" href="#">Send Your Email</a>-->
            </p>

            <div id="response">

            </div>
        </form>

    </div>

我一直试图弄清楚将近一个月。

1 个答案:

答案 0 :(得分:0)

您是否尝试在创建课程实例之前发送标题?

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

    $contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
    $contact_form->sendRequest();
    exit();