PHP表单不起作用,尽管做得对

时间:2013-12-19 00:59:29

标签: php html

我正在尝试制作PHP表单,它正在向我发送成功消息,但我没有收到任何邮件,尝试了一切但没有用。 这是我的主要PHP代码:

<?php 
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP

//If the form is submitted
if(isset($_POST['submitted'])) {

    // require a name from user
    if(trim($_POST['contactName']) === '') {
        $nameError =  'Forgot your name!'; 
        $hasError = true;
    } else {
        $name = trim($_POST['contactName']);
    }

    // require a name from user
    if(trim($_POST['phone']) === '') {
        $phoneError =  'Forgot your contact!'; 
        $hasError = true;
    } else {
        $phone = trim($_POST['phone']);
    }

    // need valid email
    if(trim($_POST['email']) === '')  {
        $emailError = 'Forgot to enter in your e-mail address.';
        $hasError = true;
    } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
        $emailError = 'You entered an invalid email address.';
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    // we need at least some content
    if(trim($_POST['comments']) === '') {
        $commentError = 'You forgot to enter a message!';
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['comments']));
        } else {
            $comments = trim($_POST['comments']);
        }
    }

    // upon no failure errors let's email now!
    if(!isset($hasError)) {

        $emailTo = 'engr.hussainraza@live.com';
        $subject = 'MATHESON FIRST AID: ENQUIRY FORM SUBMISSION '.$name;
        $sendCopy = trim($_POST['sendCopy']);
        $body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
        $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);

        // set our boolean completion value to TRUE
        $emailSent = true;
    }
}
?>

这是我的HTML代码,请注意我已将脚本和表单放在同一页面上,因此也嵌入HTML中,这里的格式为HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<title>hussain</title>

<link rel="stylesheet" href="css/style.css" type="text/css" >
    <script type="text/javascript" src="js/jquery-1.5.2.js" ></script>
</head>

<body>
        <div class="wrapper_main">

            <?php if(isset($emailSent) && $emailSent == true) {
                include 'success.html';
            } else { ?>

            <div id="content">
                <section class="section_right">
                    <img src="images/right2.png"   alt="" title="" />
                </section>
                <div class="main_content">
                    <h1>Enquiry Form</h1><br/>
                    <?php if(isset($hasError) || isset($captchaError) ) { ?>
                        <p class="alert">Error submitting the form</p>
                    <?php } ?>
                    <div class="form_wrap2">
                        <form id="contact-us" action="enquiry.php" method="post">
                            <fieldset>
                                <label for="name">Name:</label>
                                <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" placeholder="Enter your name" />
                                <?php if($nameError != '') { ?>
                                <br /><span class="error"><?php echo $nameError;?></span> 
                                <?php } ?>

                                <label for="title">Company</label>
                                <input name="title" type="text" id="company" placeholder="Enter your company name" />

                                <label for="address">Address:</label>
                                <input name="address" type="text" id="address" placeholder="Enter your address" />

                                <label for="tel">Telephone:</label>
                                <input type="text" name="phone" id="phone" value="<?php if(isset($_POST['phone'])) echo $_POST['phone'];?>" placeholder="Enter your telephone number" />
                                <?php if($phoneError != '') { ?>
                                <br /><span class="error"><?php echo $phoneError;?></span> 
                                <?php } ?>

                                <label for="email">Email:</label>
                                <input type="email" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" placeholder="Enter your email address" />
                                <?php if($emailError != '') { ?>
                                <br /><span class="error"><?php echo $emailError;?></span>
                                <?php } ?>

                                <label for="business">Type of Business:</label>
                                <input name="bussiness" type="text" id="business" placeholder="Enter type of business" />

                                <label for="enquiry">Enquiry:</label>
                                <textarea name="comments" id="commentsText" id="comments" placeholder="Enter comments here" ><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
                                <?php if($commentError != '') { ?>
                                <br /><span class="error"><?php echo $commentError;?></span> 
                                <?php } ?>

                                <input type="reset" value="Reset" />
                                <input type="submit" value="Submit" />
                                <input type="hidden" name="submitted" id="submitted" value="true" />

                            </fieldset>
                        </form>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>
    </div>
    <!-- content end -->

  </div>
</div>
<script type="text/javascript">
    <!--//--><![CDATA[//><!--
    $(document).ready(function() {
        $('form#contact-us').submit(function() {
            $('form#contact-us .error').remove();
            var hasError = false;
            $('.requiredField').each(function() {
                if($.trim($(this).val()) == '') {
                    var labelText = $(this).prev('label').text();
                    $(this).parent().append('<span class="error">Your forgot to enter your '+labelText+'.</span>');
                    $(this).addClass('inputError');
                    hasError = true;
                } else if($(this).hasClass('email')) {
                    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                    if(!emailReg.test($.trim($(this).val()))) {
                        var labelText = $(this).prev('label').text();
                        $(this).parent().append('<span class="error">Sorry! You\'ve entered an invalid '+labelText+'.</span>');
                        $(this).addClass('inputError');
                        hasError = true;
                    }
                }
            });
            if(!hasError) {
                var formInput = $(this).serialize();
                $.post($(this).attr('action'),formInput, function(data){
                    $('form#contact-us').slideUp("fast", function() {                  
                        $(this).before('<p class="tick"><strong>Thanks!</strong> Your email has been delivered. Huzzah!</p>');
                    });
                });
            }

            return false;   
        });
    });
    //-->!]]>
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

PHP的邮件功能在您的服务器上运行sendmail。很可能没有安装和/或没有运行。

答案 1 :(得分:0)

几个通知:

  1. 不要禁用error_reporting,直到你去生产。它会溢出可能出错的地方,或者你不应该这样做。无论如何你可能想要这样做,因此你可以在触发错误的命令之前放一个@。

  2. 而不是trim($_POST['contactName'] == ''使用empty($_POST['contactName'])。这样做的好处是,如果设置了值,则自动检查。如果您不想将空格作为输入,请同时使用它们。

  3. 您可以使用简单的PHP内置函数!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email'])代替filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)

  4. $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;是无效的标头,因为您没有指定名称。使用$headers = 'From: ' . $emailTo . "\r\n" . 'Reply-To: ' . $email;。有关有效标题http://php.net/manual/en/function.mail.php(参见参考号)

  5. ,请参阅

    在其顶部初始化脚本变量的一般建议。这样你只需要一直检查和修剪(?)你的输入,但只需要做一次并将其分配给一个安全的变量。