PHP表单不起作用,尽管一切都做对了


PHP form not working, although done everything right

我正在尝试制作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>

PHP的邮件函数在服务器上运行。它很可能未安装和/或未运行。

几个注意事项:

  1. 在进入生产环境之前,不要禁用error_reporting。它会溢出可能出错的地方,或者你不应该做的事情。您可能还是想这样做,因此您可以在触发错误的命令之前放置一个 @。

  2. 而不是trim($_POST['contactName'] == ''使用empty($_POST['contactName']).这样做的优点是,如果值设置,则会自动检查。如果您不希望空白区域作为输入,请同时使用两者。

  3. 代替!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+'.[a-z]{2,4}$/i", trim($_POST['email']),可以使用简单的PHP内置函数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(到参数)

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