1

我的网站上有此联系表,但我无法检查表单字段是否已完成。

<form id="cform" name="cform" method="post" action="<?php bloginfo('template_directory'); ?>/cform.php">
  <div id="nande">
    <p><input type="text" id="name" name ="name"; onfocus="if(this.value=='Your Name is')this.value='';" onblur="if(this.value=='')this.value='Your Name is';" alt="Your Name" value="Your Name is"/></p>
    <p> <input type="text" id="e-mail" name="mail" onfocus="if(this.value=='Your Email is')this.value='';" onblur="if(this.value=='')this.value='Your Email is';" alt="Your Email" value="Your Email is"/></p>
  </div>
  <div class="clear"></div>
  <p><textarea name="message" rows="4" id="comments" tabindex="4" title="comments" onfocus="if(this.value=='Your Message is')this.value='';" onblur="if(this.value=='')this.value='Your Message is';"">Your Message is</textarea></p>
  <div class="clear"></div>
  <p><input name="submit" type="submit" value="SEND" id="submit"/></p>
</form> 

下面是表单的 PHP

<?php
if(isset($_POST['submit'])) {
   $to = 'email@address.com' ;     //put your email address on which you want to receive the information
   $subject = 'designdone project email';   //set the subject of email.
   $headers  = 'MIME-Version: 1.0' . "\r\n";
   $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
   $message = "<table><tr><td>Your Name</td><td>".$_POST['name']."</td></tr>
               <tr><td>E-Mail</td><td>".$_POST['email']."</td></tr>
               <tr><td>Message</td><td>".$_POST['message']."</td>
               </tr></table>" ;
mail($to, $subject, $message, $headers);
header( 'Location: http://designdone.com' ) ;
}
?>

如何检查用户是否填写了表单并且没有发送空表单?

感谢您的帮助。

4

3 回答 3

3

尝试在字段上使用isset()或。empty()$_POST

例如:

foreach ( $_POST as $attr => $val ) {
  if ( !isset( $_POST[ $attr ] ) || empty( $_POST[ $attr ] ) ) {
    die( 'Please fill out all fields.' );
  }
}
于 2012-06-08T13:25:49.047 回答
1

我建议使用验证库。有很多可用的,而且大多数都非常灵活。

于 2012-06-08T13:28:35.947 回答
0

在 javascript 中,您可以使用 .isEmpty 来确定该字段是否为空。

于 2012-06-08T13:30:03.977 回答