2015-02-23 81 views
0

我做的提交表单,多数民众赞成的PHP检查,我想向用户展示他们忘记填写所有字段的PHP检查所有空的输入列表。我也在使用jQuery,但用户可以禁用它,而且您确实需要服务器端检查表单。如何显示对形式

的事情是,如果有6个必填字段,他们提交的形式与他们的2个空,代码只显示第一位的,之后便再次提交,它会显示他们的第二个。你会建议做什么?

下面是代码:

if (isset($_POST['submit'])){ 

     $message = ""; 

     if (trim($_POST['ign'])){ 
      if (trim($_POST['god'])){ 
       if (trim($_POST['replay_id'])){ 
        if (trim($_POST['map_type'])){ 
         if (trim($_POST['time_min']) AND trim($_POST['time_sec'])){ 
          if (trim($_POST['description'])){ 
           // Submit the form 
          }else{ 
           $message .= "<li>Description is empty</li>"; 
          } 
         }else{ 
          $message .= "<li>Time not specified</li>"; 
         } 
        }else{ 
         $message .= "<li>Match type not specified.</li>"; 
        } 
       }else{ 
        $message .= "<li>Replay ID not specified.</li>"; 
       } 
      }else{ 
       $message .= "<li>God was not specified.</li>"; 
      } 
     }else{ 
      $message .= "<li>In game name was not specified!</li>"; 
     } 


     if (!empty($message)){ 
      $message = "<div style='text-align:left; display: inline-block;'><ul>".$message."</ul></div>"; 
     }else{ 
      $message = "Submit succesfull"; 
     } 

     echo "<div id='close-message'><div class='admin-message'>$message</div></div>"; 
    } 

这样做的,我能想到的,如果这个代码的唯一的其他方式:

if (isset($_POST['submit'])){ 

    $message = ""; 
    $pass = TRUE; 

    if (!trim($_POST['ign'])){   $message .= "<li>In game name was not specified!</li>"; $pass = FALSE; } 
    if (!trim($_POST['god'])){   $message .= "<li>God was not specified.</li>";   $pass = FALSE; } 
    if (!trim($_POST['replay_id'])){ $message .= "<li>Replay ID not specified.</li>";  $pass = FALSE; } 
    if (!trim($_POST['map_type'])){  $message .= "<li>Match type not specified.</li>";  $pass = FALSE; } 
    if (!trim($_POST['description'])){ $message .= "<li>Description is empty</li>";   $pass = FALSE; } 
    if (!trim($_POST['time_min']) AND trim($_POST['time_sec'])){ $message .= "<li>Time not specified</li>"; $pass = FALSE; } 

    if ($pass){ 
     $message = "Submit succesfull"; 
     // Submit the form 
    }else{ 
     $message = "<div style='text-align:left; display: inline-block;'><ul>".$message."</ul></div>"; 
    } 

    echo "<div id='close-message'><div class='admin-message'>$message</div></div>"; 
} 

是否有其他办法做到这一点?同样,纯粹的PHP,jQuery在那里,但它可以被禁用,HTML5也不适用于所有浏览器。

谢谢。

回答

1

如果你追求是摆脱所有if声明,并使您的代码更干净,这可能是一个可能的解决方案:

设置一个数组,其中包含必需的字段及其相应的“必需”错误消息。循环通过阵列和匹配字段针对$_POST察觉这是没有设置任何所需的字段,加入任何此类字段的错误消息发送到所述$errors阵列。

发现的所有错误将被显示为一个无序列表。如果没有错误,则会显示成功消息。

// Set some values for the example 
$_POST['submit'] = '1'; 
$_POST['ign'] = 'foo'; 
$_POST['god'] = 'bar'; 
$_POST['description'] = 'baz'; 

// Validate required fields if submitted 
if (isset($_POST['submit'])) { 
    $required = array(
     'ign'   => 'In game name was not specified!', 
     'god'   => 'God was not specified', 
     'replay_id' => 'Replay ID not specified', 
     'map_type' => 'Match type not specified', 
     'description' => 'Description is empty', 
     'time_min' => 'Time not specified', 
     'time_sec' => 'Time not specified', 
    ); 
    $errors = array(); 

    foreach ($required as $field => $errorMessage) { 
     if (isset($_POST[$field]) && trim($_POST[$field]) != '') { 
      continue; // All is well, check next required field 
     } 

     // No value was set for this required field 
     $errors[] = $errorMessage; 
    } 

    if ($errors) { 
     // Show any errors (use array_unique() to avoid duplicate error messages 
     // on time_min/time_sec fields) 
     $message = "" 
      . "<div style='text-align:left; display: inline-block;'>" 
      .  "<ul>" 
      .   "<li>" 
      .    implode('</li><li>', array_unique($errors)) 
      .   "</li>" 
      .  "</ul>" 
      . "</div>"; 
    } 
    else { 
     // All is well 
     $message = "Submit successful"; 
    } 

    echo "" 
     . "<div id='close-message'>" 
     .  "<div class='admin-message'>$message</div>" 
     . "</div>"; 
} 

输出(缩进来提高可读性):

<div id='close-message'> 
    <div class='admin-message'> 
     <div style='text-align:left; display: inline-block;'> 
      <ul> 
       <li>Replay ID not specified</li> 
       <li>Match type not specified</li> 
       <li>Time not specified</li> 
      </ul> 
     </div> 
    </div> 
</div> 
+0

等什么?此代码如何将错误消息添加到错误列表中?或者继续实际上意味着,它会忽略其余的代码,并立即开始在数组中的新项目?此外,Isnt循环比在我的第二个代码中使用这些ifs更具资源要求? Thanyway,谢谢你的回应,我没有想到阵列。 – MiChAeLoKGB 2015-02-23 20:58:19

+0

是的,这是'继续'如何工作。至于你的其他问题,我会说,在处理这种代码时,循环和重复if语句之间的纳秒性能差异是不成问题的。可读性,可维护性和清晰的结构更重要。 – mhall 2015-02-23 21:17:06

+0

啊,我其实从来没有使用过继续,所以从来没有打扰到它究竟发生了什么:D另外,这是真的,所以病态可能使用数组:)谢谢。 – MiChAeLoKGB 2015-02-23 21:29:55

1

我宁愿把我的错误消息旁边导致错误的领域和运行基于阵列上的整个事情保持代码的DRY:

if (isset($_POST['submit'])){ 

    $fields = array(
     'ign' => 'In game name was not specified!', 
     'god' => 'God was not specified.', 
     ... 
    ); 
    $message = ""; 
    $error = array(); 
    $pass = TRUE; 

    foreach ($fields as $fld => $errmsg) { 
     if (!trim($_POST[$fld])) { 
      $message .= "<li>$errmsg</li>"; 
      $error[$fld] = $errmsg; 
      $pass = FALSE; 
     } 
    } 

我离开你$message变量在那里,因为它可能会让用户很好地在表单顶部和任何错误字段旁边获取消息。通常我会说“您的表单提交有错误 - 请参阅下文”。

然后在你的形式显示代码显示的$error[$fld]值适当地向下。 (我假设你是再次显示形式,让广大用户解决他们并没有在第一时间满山遍野。)

所以之前你表单可能是这个样子的(如果你使用的是表):

<table> 
    <tr> 
    <td align="right">In Game Name:</td> 
    <td><input name='ign' type='text' /></td> 
    </tr> 
    ... 

,现在它看起来就像这样:

<table> 
    <tr> 
    <td align="right">In Game Name:</td> 
    <td><input name='ign' type='text' /></td> 
    <td><?= @$error['ign'] ?></td> 
    </tr> 
    ... 

随着工作非常点点,你可以风格你的字段标签用一种颜色来吸引用户的关注和等

当然,如果你的表格是简单的,那么你可以有另一个阵列(或延伸超过$ fields数组的),这将再次做这一切为你在一个简单的循环:

$fields = array(
    'ign' => array('label' => 'In Game Name:', 'error' => 'In game name was not specified!'), 
    'god' => array('label' => 'God Mode:', 'error' => 'God was not specified.'), 
    ... 
); 
+0

我实际上是把红色的边框是空的所有字段/包含,而不是把下一另一个错误信息给他们的错误,因为形式和整个页面必须有响应,所以我不能承担它太宽。但那实际上是相当不错的答案。 – MiChAeLoKGB 2015-02-23 21:32:43