首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP验证-检查是否填充了3个字段中的一个

PHP验证-检查是否填充了3个字段中的一个
EN

Stack Overflow用户
提问于 2015-11-20 01:06:37
回答 4查看 65关注 0票数 0

我试图验证三个相关的字段。这3个字段的形式已经有几十个字段,其中许多字段正在验证。

对于所讨论的3个相关字段,我需要编写代码,检查其中的一个字段是否已填充,如果其他两个字段中的任何一个未填充,则抛出一个错误。

  • 因此,如果A被填充,而B和C不是= error。
  • 如果B被填充,而A和C不= error。
  • 如果填充了C,而A和B不= error。 然后再加上两个和第三个,没有。

例如:如果A和B被填充,而C不是= error。

这里是我开始的地方,但不确定这是否正确的验证:

代码语言:javascript
运行
复制
if(!empty($post['email_notify_subject']) 

    && empty($post['email_notify_date']) 
    && empty($post['email_notify_emails']))

{

$errors['email_notify_date'] = 'Enter Notify Date'; 
$errors['email_notify_emails'] = 'Enter Notify Emails';

} 

else if (empty($post['email_notify_subject']) 

    && !empty($post['email_notify_date']) 
    && empty($post['email_notify_emails'])) 

{

$errors['email_notify_subject'] = 'Enter Notify Subject';
$errors['email_notify_emails'] = 'Enter Notify Emails';

} 

else if (empty($post['email_notify_subject']) 

    && empty($post['email_notify_date']) 
    && !empty($post['email_notify_emails'])) 

{

$errors['email_notify_subject'] = 'Enter Notify Subject';
$errors['email_notify_date'] = 'Enter Notify Date';

}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-11-20 01:16:11

这怎么回事?

代码语言:javascript
运行
复制
if (
    (
        ! empty($post['email_notify_subject']) &&
        empty($post['email_notify_date']) &&
        empty($post['email_notify_emails'])
    ) ||
    (
        empty($post['email_notify_subject']) &&
        ! empty($post['email_notify_date']) &&
        empty($post['email_notify_emails'])
    ) ||
    (
        empty($post['email_notify_subject']) &&
        empty($post['email_notify_date']) &&
        ! empty($post['email_notify_emails'])
    )
) {
    // only 1 field filled out
}

或者还有另一种方法:

代码语言:javascript
运行
复制
$num_filled_out = 0;
if ( ! empty($post['email_notify_subject']) ) $num_filled_out++;
if ( ! empty($post['email_notify_date']) ) $num_filled_out++;
if ( ! empty($post['email_notify_emails']) $num_filled_out++;
if ($num_filled_out <= 1) {
    // 1 or 0 filled out
}
票数 0
EN

Stack Overflow用户

发布于 2015-11-20 01:19:53

没有对此进行测试,但是尝试一下:

代码语言:javascript
运行
复制
<?php
  if(empty(array(
    $_post['email_notify_subject'],
    $_post['email_notify_date'],
    $_post['email_notify_emails']))){
      echo "All Three filed required"; //or other instructions
}
  else{
   //Proceed with the form
}
票数 0
EN

Stack Overflow用户

发布于 2015-11-20 01:21:02

这将是一种更好的方法,因为您可以在一个数组中检查所有要检查的字段和所有相应的错误消息。而且,它的可伸缩性更强。

请注意,您必须测试表单method属性是否设置为post,并且为输入字段指定了正确的名称。

代码语言:javascript
运行
复制
$data_to_check = array(
    'email_notify_subject'=>array(
        'error'=>'Enter Notify Subject'
    ),
    'email_notify_date'=>array(
        'error'=>'Enter Notify Date'
    ),
    'email_notify_emails'=>array(
        'error'=>'Enter Notify Emails'
    ),
);

$errors = array();

foreach($data_to_check as $key => $value) {
    if (empty($post[$key])) {
        $errors[] = $value['error'];
    }
}

if (!empty($errors)) {
    foreach($errors as $key => $value) {
        echo '<p class="error">'.$value.'</p>';
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33817294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档