【问题标题】:PHP Validation to require One of Two Form Fields filled inPHP验证要求填写两个表单字段之一
【发布时间】:2014-03-23 17:21:17
【问题描述】:

我正在开发一个网站,该网站使用 javascript 来处理许多功能,并使用基于 PHP 的验证码来验证表单字段。

效果很好,但是如果没有填写任何字段,表单将提交。

我需要填写两个表单字段 ['email' 或 'phone'] 之一,但两者都不能留空。

错误消息可能与验证码字段留空或填写错误时抛出的错误消息相同。

我是 PHP 代码新手,终生无法弄清楚如何调用该函数。

功能码为:

 <?php
 if(isset($_POST['send'])){
  $emailFrom = "clientemail.com";
  $emailTo = "clientemail.com";
  $subject = "Contact Form Submission";

  $first_name = strip_tags($_POST['first_name']); 
  $last_name = strip_tags($_POST['last_name']); 
  $email = strip_tags($_POST['email']); 
  $phone = strip_tags($_POST['phone']);
  $message = strip_tags(stripslashes($_POST['message'])); 

  $body .= "First Name: ".$first_name."\n";
  $body .= "Last Name: ".$last_name."\n";
  $body .= "Email: ".$email."\n";
  $body .= "Phone: ".$phone."\n";
  $body .= "Comments: ".$message."\n";


  $headers = "From: ".$emailFrom."\n";
  $headers .= "Reply-To:".$email."\n";  

  if($_SESSION['security_code'] == $_POST['security_code']){
    $success = mail($emailTo, $subject, $body, $headers);
    if ($success){
    echo '<p class="yay">Your e&ndash;mail has been sent.</p>';
    } 
  } else {
    echo '<p class="oops">Something went wrong. Hit the back button and try again.</p>';
  }
 } else {
 ?>

表单域:

    <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id="contact" name="contact">

      <fieldset>
       <label for="name"><span style="color:#bf252b;">*</span>First Name</label>
       <input type="text" id="first_name" name="first_name"  minlength="2"/>
       <label for="name">Last Name</label>
       <input type="text" id="last_name" name="last_name" minlength="2"/>
       <label for="email"><span style="color:#bf252b;">*</span> Email</label>
       <input type="text" id="email" name="email"   />
       <label for="phone"><span style="color:#bf252b;">*</span> Phone</label>
       <input type="text" id="phone" name="phone"  />
       <label for="message">Message</label><div style="clear:both;"></div>

       <textarea id="message" name="message" cols="40" rows="10" ></textarea>

       <img src="../captcha.php" id="captcha" alt="captcha" style="padding:25px 0px 20px 0px;" />
       <label for="security_code">Enter captcha</label>
       <input type="text" id="security_code" name="security_code" autocomplete="off" class="required"/>

       <button type="submit" id="send" name="send" style="margin:0px 0px 10px 12px;">Send!</button>

     </fieldset>
  </form> 

<?php } ?> 

有一个运行验证码的 .php 文档,但我认为有一个简单的解决方案是对的吗?现有代码中的一些额外代码可以解决我的问题吗?如果可以的话,我真的很想避免使用 javascript 和插件。

提前致谢!!

【问题讨论】:

    标签: php html forms field required


    【解决方案1】:

    试试这个,

    if($_SESSION['security_code'] == $_POST['security_code'] && (!empty($email) || !empty($phone))) {
    

    而不是

    if ($_SESSION['security_code'] == $_POST['security_code']) {
    

    这不是验证表单的便捷方式,但我希望这会有所帮助。

    【讨论】:

    • 它可以工作,但需要填写两个字段,否则会抛出错误消息。它肯定更接近我的需要! :)
    • 对不起,我看错了你的问题。我已编辑答案,请重试。
    • 太好了!谢谢你萨米尔!!
    【解决方案2】:

    required 属性添加到您需要的字段中。

    【讨论】:

      【解决方案3】:

      修复你的代码!

      Notice: Undefined variable: body in /../sendform.php on line 14
      

      这样做:

      $body = "First Name: ".$first_name."\n";
      $body .= "Last Name: ".$last_name."\n";
      $body .= "Email: ".$email."\n";
      $body .= "Phone: ".$phone."\n";
      $body .= "Comments: ".$message."\n";
      

      解决方案

      if(($_SESSION['security_code'] == $_POST['security_code']) AND 
        (!empty($email) OR !empty($phone)) ) {
      

      换句话说:

      • 安全码必须匹配
      • 电子邮件或电话不能为空

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-08
        • 2013-12-29
        • 1970-01-01
        • 2017-01-23
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        相关资源
        最近更新 更多