php - 联系表 : Why won't this form validate?

标签 php jquery validation forms contact-form

我在(单页业务/投资组合)网站底部有一个联系表格。

我的 DOCTYPE 上面的脚本如下所示。

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

    //Check to make sure that the name field is not empty
    if(trim($_POST['contactname']) == '') {
        $hasError = true;
    } else {
        $name = trim($_POST['contactname']);
    }

    //Check to make sure that the subject field is not empty
    if(trim($_POST['subject']) == '') {
        $hasError = true;
    } else {
        $subject = trim($_POST['subject']);
    }

    //Check to make sure sure that a valid email address is submitted
    if(trim($_POST['email']) == '')  {
        $hasError = true;
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$",   trim($_POST['email']))) {
        $hasError = true;
    } else {
        $email = trim($_POST['email']);
    }

    //Check to make sure comments were entered
    if(trim($_POST['comment']) == '') {
        $hasError = true;
    } else {
        if(function_exists('stripslashes')) {
            $comments = stripslashes(trim($_POST['message']));
        } else {
            $comments = trim($_POST['message']);
        }
    }

    //If there is no error, send the email
    if(!isset($hasError)) {
        $emailTo = 'myemail@gmail.com'; //Put your own email address here
        $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
        $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);
        $emailSent = true;
    }
}
?>

JQuery 验证确实有效。

<script type="text/javascript">
$(document).ready(function(){
    $("form").validate();
});
</script>

联系表格:

<div class="grid_8">
    <?php if(isset($hasError)) { //If errors are found ?>
        <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
    <?php } ?>

    <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
        <p><strong>Email Successfully Sent!</strong></p>
        <p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
    <?php } ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
        <div> 
            <label for="name">Your Name:</label> 
            <div> 
                <input type="text" name="contactname" class="required" /> 
            </div> 
        </div> 
        <div> 
            <label for="email">Your Email:</label> 
            <div> 
                <input type="text" name="email" class="required email" /> 
            </div>   
        </div> 
        <div> 
            <label for="subject">Subject:</label> 
            <div> 
                <input type="text" name="subject" class="required" /> 
            </div>
        </div> 
        <div> 
            <label for="comments">Comments:</label> 
            <div> 
                <textarea name="comment" name="comment" class="required"></textarea> 
            </div> 
        </div> 
        <div> 
            <input id="button" type="submit" value="SEND" /> 
        </div> 
    </form>
    </div>

当您提交表单时,不会发送电子邮件,也不会收到 php 的任何验证。我做错了什么?

最佳答案

尽管我有很多不太关键的问题,但还是有一个明显的问题。简而言之,您要检查 $_POST['submit'] 并在表单中添加 name="submit"

所以改变:

 <input id="button" type="submit" value="SEND" />

致:

 <input id="button" type="submit" name="submit" value="SEND" />

或者更改:

 if(isset($_POST['submit']))

致:

 if(count($_POST))
 // OR
 if(!empty($_POST))

任何一个都可以解决您的问题

关于php - 联系表 : Why won't this form validate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5195745/

相关文章:

php - 通过 Ajax 将 Javascript 对象发送到 PHP

php - LAMP/Laravel - 报告生成最大化单个 CPU

javascript - JQuery - 禁用表单,除非值不为 NULL?

jquery - onload动画会影响SEO吗?

JavaScript 和 jQuery

python - 询问用户输入,直到他们给出有效的响应

php - 如何从职业解决方案表中的用户那里获取角色?

javascript - 单击时 jQuery 切换 div 类不起作用

php - 如何在 PHP 中验证表单

c++ - C/C++,如何在等待用户输入 int 、 float 或其他任何内容时拒绝输入 char/string?