duanniwu7730 2014-03-23 17:21
浏览 47
已采纳

PHP验证要求填写两个表单字段之一

I am developing a site that uses javascript to handle many functions and PHP based Captcha code to validate the form field.

It works great, but the form will submit if none of the fields are filled in.

I need one of two form fields ['email' or 'phone'] filled in, but neither can be left blank.

The error message can be the same error message thrown up when the captcha field is left blank or filled in incorrectly.

I am new to PHP code and cannot figure out for the life of me how to call the function.

The function code is:

 <?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."
";
  $body .= "Last Name: ".$last_name."
";
  $body .= "Email: ".$email."
";
  $body .= "Phone: ".$phone."
";
  $body .= "Comments: ".$message."
";


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

  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 {
 ?>

The form field:

    <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 } ?> 

There is a .php document for running captcha, but am I right in thinking there is a simple solution for this; some extra code in the existing code that will fix my issue? I really want to avoid javascript and plugins if I can help it.

Thanks in advance!!

  • 写回答

3条回答 默认 最新

  • doufu3718 2014-03-23 17:30
    关注

    Try this,

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

    instead of

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

    This is not convenient way to validate form but I hope this will help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计