联系表格显示PHP文件

时间:2017-08-23 07:28:19

标签: php html email

目前我正在我的网站上开发一个联系表格。这里我附上了我的代码

HTMl文件

<form action="php/contacts-process.php"  class="contacts-process" method="post"  novalidate="novalidate">
                    <div class="col-md-12 col-sm-12">
                        <input type="text" class="form-control" placeholder="Name" name="name" >
                    </div>
                    <div class="col-md-12 col-sm-12">
                        <input type="text" class="form-control" placeholder="Phone" name="phone" >
                    </div>
                    <div class="col-md-12 col-sm-12">
                        <input type="email" class="form-control" placeholder="Email" name="email" >
                    </div>
                    <div class="col-md-12 col-sm-12">
                        <textarea class="form-control" placeholder="Message" rows="7" name="message" ></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 MESSAGE">
                    </div>
                </form>

PHP文件

<?php
if(!session_id()) {
    session_start();
}
error_reporting(0);

if (isset($_REQUEST['action'])) {
    if ($_REQUEST['action'] == "email_server_responce") {
        $ourMail = "xyz@gmail.com"; //Insert your email address here
        $pre_messagebody_info = "";
        $errors = array();
        $data = array();
        parse_str($_REQUEST['values'], $data);

        $result = array(
            "is_errors" => 0,
            "info" => ""
        );

        if (!empty($errors)) {
            $result['is_errors'] = 1;
            $result['info'] = $errors;
            echo json_encode($result);
            exit;
        }

        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers.= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers.= "From: ".$data['email']."\r\n";
        $pre_messagebody_info.="<strong>Name</strong>" . ": " . $data['name'] . "<br />";
        if (! empty($data['email']) ) {
            $pre_messagebody_info.="<strong>E-mail</strong>" . ": " . $data['email'] . "<br />";
        }
        if (! empty($data['phone']) ) {
            $pre_messagebody_info.="<strong>Phone</strong>" . ": " . $data['phone'] . "<br />";
        }
        if (! empty($data['url']) ) {
            $pre_messagebody_info.="<strong>URL</strong>" . ": " . $data['url'] . "<br />";
        }
        if (empty($data['subject']) ) {
            $subject = "Website Contact Form";
        } else {
            $subject = $data['subject'];
        }
        $after_message = "\r\n<br />--------------------------------------------------------------------------------------------------\r\n<br /> This mail was sent via contact form";

        if (mail($ourMail, $subject, $pre_messagebody_info .= $category . "<strong>Message</strong>" . ": " . $data['message'] .$after_message, $headers)) {
            $result["info"] = "success";
        } else {
            $result["info"] = "server_fail";
        }
        echo json_encode($result);
        exit;
    }
} ?>

当我点击提交按钮时,它会在我的浏览器上显示php代码。我无法弄清楚我的代码中出了什么问题。我正在运行tomcat服务器。任何人都可以查看它,请告诉我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

服务器未正确配置为运行php;)

您可以查看@Ahmed Sunny建议的参考Run a php app using tomcat?