HTML表单提交按钮显示PHP代码而不是执行它

时间:2014-04-26 23:52:49

标签: php forms xhtml

我刚刚使用HTML编写了第一个表单,从另一个站点获取了一封php电子邮件表单的模板。但是,当我输入所有数据时,它只显示php页面而不是执行脚本。

            <form action="template1.php" method="post">
            <p style="padding-top: 5px;" id="form_header">
                Fill out variable information below:
            </p>
            <table width="260px" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td style="padding-top: 14px">
                        Customer name:
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="toName" type="text" size="45" />
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 14px">
                        Customer e-mail address:
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="toEmail" type="text" size="45" />
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 14px">
                        From name:
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="fromName" type="text" size="45" />
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 14px;">
                        From e-mail address:
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="fromEmail" type="text" size="45" value="place@holder.com" />
                    </td>
                </tr>
            </table>
            <table id="reason_boxes" width="260px" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td style="width: 70px; height: 30px;">
                        Reason:
                    </td>
                    <td style="padding: 0 4px 4px 4px; vertical-align: center;">
                        <input type="radio" name="reason" value="credit"/>
                    </td>
                    <td style="padding: 1px 0 4px 4px; vertical-align: center;">
                        Insufficient credit.
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 14px; width: 70px; height: 100px;">
                    </td>
                    <td style="padding: 4px;">
                        <input type="radio" name="reason" value="credit"/>
                    </td>
                    <td style="padding: 4px;">
                        Software based POS systems and/or Payment Jack processing units do not qualify. We will continue to make enhancements to this service and hope to be able to offer this in the future.
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 14px; width: 70px; height: 100px;">
                    </td>
                    <td style="padding: 4px;">
                        <input type="radio" name="reason" value="credit"/>
                    </td>
                    <td style="padding: 4px;">
                        Mail order/telephone order and/or internet based business does not qualify. We will continue to make enhancements to this service and hope to be able to offer this in the future.
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 14px; width: 70px; height: 70px;">
                    </td>
                    <td style="padding: 4px;">
                        <input type="radio" name="reason" value="credit"/>
                    </td>
                    <td style="padding: 4px;">
                        The average ticket requested currently exceeds the parameters established for this program.
                    </td>
                </tr>
                <tr>
                    <td style="padding-top: 14px; height: 50px;">
                    </td>
                    <td style="padding: 4px;">
                    </td>                        
                    <td style="padding: 4px; text-align: right; vertical-align: bottom;">
                        <input name="submit" type="submit" value="Send Email" />
                    </td>
                </tr>
            </table>

和PHP

    <?php

if(isset($_POST['toEmail'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = $_POST['toName'];;
    $email_subject = "This is a test";

    function died($error) {
        // your error code can go here
        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();
    }

    // validation expected data exists
    if(!isset($_POST['toName']) ||
        !isset($_POST['toEmail']) ||
        !isset($_POST['fromName']) ||
        !isset($_POST['fromEmail'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $to_name = $_POST['toName']; // required
    $to_email = $_POST['toEmail']; // required
    $from_name = $_POST['fromName']; // required
    $from_email = $_POST['fromEmail']; //  required

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

  if(!preg_match($email_exp,$to_email)) {
    $error_message .= 'The \'to\' email address you entered for the customer does not appear to be valid.<br />';
  }

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

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

  if(!preg_match($string_exp,$toName)) {

    $error_message .= 'The first name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$toEmail)) {

    $error_message .= 'The last name you entered does 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 .= "toName: ".clean_string($toName)."\n";

    $email_message .= "toName: ".clean_string($toEmail)."\n";

    $email_message .= "fromName: ".clean_string($fromName)."\n";

    $email_message .= "fromEmail: ".clean_string($fromEmail)."\n";





// create email headers

$headers = 'From: '.$from_name."\r\n".

'Reply-To: '.$from_email."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->



Thank you for contacting us. We will be in touch with you very soon.



<?php

}

?>

谢谢!

1 个答案:

答案 0 :(得分:1)

好像你的服务器没有安装php。如果安装了mime类型的检查服务器设置。扩展名.php需要正确映射。

以下是为扩展程序设置mime类型的示例:Custom MIME Type for PHP File