2011-03-04 122 views
1

我在(单页业务/投资组合)网站底部有一个联系表单。联系表格:为什么这个表格不能验证?

我的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._%-][email protected][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 = '[email protected]'; //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验证。我究竟做错了什么?

+0

请注意eregi()自PHP 5.3.0起弃用 – 2011-03-04 15:32:30

回答

2

一个快速明显的问题,虽然我有很多不太重要那些。只要你检查$_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)) 

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

2

我不明白为什么isset($_POST['submit'])应该在表单中没有name="submit"的字段时返回true。

我喜欢为所有需要的数据做的是使用一个数组:

<!-- ... --> 
<input type="text" name="mail[contactname]" class="required" /> 
<!-- ... --> 
<input type="text" name="mail[email]" class="required email" /> 
<!-- ... --> 
<input type="text" name="mail[subject]" class="required" /> 
<!-- ... --> 
<textarea name="comment" name="mail[comment]" class="required"></textarea> 
<!-- ... --> 

然后要求该阵列

if (isset($_POST['mail'])) 
    // ...