HTML / PHP电子邮件表单仅显示PHP代码

时间:2017-11-01 17:05:07

标签: php html

我目前正在尝试使用HTML创建自己的网站,我想要添加电子邮件联系表单。我不知道它是否有任何区别,但我只在我的PC上有网站,而不是在Web服务器上。所以我的问题是每当我按下提交时,浏览器只显示PHP代码,没有任何反应。

这是我的HTML代码:

    <!DOCTYPE html>
    <html>

    <head>
        <title>3D Maker Cave</title>
        <link rel="stylesheet" type="text/css" href="style.css" />
    </head>

    <body>

        <header>
            <img src="bilder/logo.png" alt="LOGO">
        </header>




        <section></section>

        <aside>
            <p>Contact</p>

            <form name="contactform" method="post" action="email_submit.php">

            <table width="400px">

            <tr>
             <td valign="top">
             <label for="name">Name *</label>
             </td>
            <td valign="top">
              <input  type="text" name="first_name" maxlength="50" size="25">
             </td>
            </tr>

            <tr>
             <td valign="top">
            <label for="email">Email Address *</label>
             </td>
             <td valign="top">
              <input  type="text" name="email" maxlength="80" size="25">
            </td>
            </tr>

            <tr>
             <td valign="top">
             <label for="message">Message *</label>
             </td>
             <td valign="top">
              <textarea  name="comments" maxlength="1000" cols="26" rows="7">                </textarea>
             </td>
            </tr>

            <tr>
             <td colspan="2" style="text-align:center">
             <input id="button" type="submit" value="Submit"> 
             </td>
            </tr>

            </table>

            </form>

        </aside>

        <footer></footer>

    </body>

    </html>

这是我的PHP代码:

    <?php
    if(isset($_POST['email'])) {

        $email_to = "my-email@gmx.de";
        $email_subject = "Website Support";

        function died($error) {
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }


        if(!isset($_POST['name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['message'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }



        $name = $_POST['name']; 
        $email = $_POST['email']; 
        $message = $_POST['message']; 

        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

      if(!preg_match($email_exp,$email)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }

        $string_exp = "/^[A-Za-z .'-]+$/";

      if(!preg_match($string_exp,$name)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br />';
      }

      if(strlen($message) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }

      if(strlen($error_message) > 0) {
        died($error_message);
      }

        $email_message = "Form details below.\n\n";


        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }



        $email_message .= "Name: ".clean_string($name)."\n";
        $email_message .= "Email: ".clean_string($email)."\n";
        $email_message .= "Message: ".clean_string($message)."\n";

    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
    ?>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="refresh" content="0; URL=http://www.google.com "> 

    <?php

    }
    ?>

0 个答案:

没有答案