表单提交和邮件发送PHP

时间:2014-01-15 23:56:24

标签: php mysql forms

不确定我做错了什么,但每次我在表单上点击提交它所做的就是打开一个新窗口,显示所有的PHP代码并且没有发送电子邮件...任何帮助将不胜感激,我我是PHP的新手,这是我的第一个脚本,所以请保持温和。

这是我的代码:

HTML

<div id="demoForm">
    <div id="fHeader"><a href="#"><Strong>Demo Request</Strong></a></div>
    <form id="dForm" action="sendDemo.php" method="post">
        <table>
            <tr>
                <td>Name : </td>
                <td><input type="text" name="name" width="200"/></td>
            </tr>
            <tr>
                <td>Company : </td>
                <td><input type="text" name="company" width="200"/></td>
            </tr>
            <tr>
                <td>Email : </td>
                <td><input type="email" name="email" width="200"/></td>
            </tr>
            <tr>
                <td>Demo Model : </td>
                <td><select name="model" id="model">
                        <option value=""></option>
                        <option value="XT3215">XT3215</option>
                        <option value="XT4015">XT4015</option>
                     </select></td>
            </tr>
            <tr>
                <td>Operating System : </td>
                <td><select name"OS" id="OS">
                        <option value=""></option>
                        <option value="POSReady_2009">POSReady 2009</option>
                        <option value="POSReady_7">POSReady 7</option>
                        <option value="Windows_7">Windows 7</option>
                        <option value="Windows_XP">Windows XP</option>
                        <option value="Linux">Linux</option>
                   </select></td>
            </tr>
            <tr>
                <td>Comments : </td>
                <td><textarea name="comments">Comments...</textarea></td>
            </tr>
        </table>
        <div id="submitBtn"><input type="submit" value="SUBMIT"/></div>
    </form>
</div>

PHP代码文件名:&#39; sendDemo.php&#39;

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

    $email_to = "xyz@12345.com"; //i have a valid email in my code. Excluded here for obvious reasons
    $email_subject = "Demo Request for ";

    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();
    }

    if(!isset($_POST['name']) ||
        !isset($_POST['company']) ||
        !isset($_POST['email']) ||
        !isset($_POST['model']) ||
        !isset($_POST['OS']) ||
        !isset($_POST['comments'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $name = $_POST["name"];
    $company = $_POST["company"];
    $email = $_POST["email"];
    $model = $_POST["model"];
    $os = $_POST["OS"];
    $comments = $_POST["comments"];

    $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(!preg_match($string_exp, $company)){
        $error_message .= 'The Company you entered does not appear to be valid.<br/>';
    }
    if(strlen($comments) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }

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

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

    $email_subject .= $model;

    $body = "Demo Request for: \n\n";
    $body .= "Name : " .clean_string($name)."\n";
    $body .= "Company : " .clean_string($company)."\n";
    $body .= "Email : " .clean_string($email)."\n";
    $body .= "Model : " .$model."\n";
    $body .= "Operating System : " .$os."\n";
    $body .= "Comments : " .clean_string($comments)."\n";

    $headers = 'From: ' .$email."\r\n";

    @mail($email_to, $email_subject, $body, $headers);
}
?>

0 个答案:

没有答案