寻找一种优雅的方法来检查表单

时间:2016-05-03 14:46:11

标签: php

有人可以建议一个更优雅的方式检查表单填写提交与PHP,我的方式似乎太长了啰嗦。我可能还需要其他类型的检查,例如,如果QA1设置为2,则应填充QA1C,如果不是则附加错误。

if (empty($_POST['QA1'])) {$errorMessage .= "<h1>QA1 not specified</h1>";}
if (empty($_POST['QA2'])) {$errorMessage .= "<h1>QA2 not specified</h1>";}
if (empty($_POST['QA3'])) {$errorMessage .= "<h1>QA3 not specified</h1>";}
if (empty($_POST['QB1'])) {$errorMessage .= "<h1>QB1 not specified</h1>";}
if (empty($_POST['QB2'])) {$errorMessage .= "<h1>QB2 not specified</h1>";}
if (empty($_POST['QB3'])) {$errorMessage .= "<h1>QB3 not specified</h1>";}
if (empty($_POST['QB4'])) {$errorMessage .= "<h1>QB4 not specified</h1>";}
if (empty($_POST['QB5'])) {$errorMessage .= "<h1>QB5 not specified</h1>";}
if (empty($_POST['QB6'])) {$errorMessage .= "<h1>QB6 not specified</h1>";}
if (empty($_POST['QB7'])) {$errorMessage .= "<h1>QB7 not specified</h1>";}
if (empty($_POST['QB8'])) {$errorMessage .= "<h1>QB8 not specified</h1>";}
if (empty($_POST['QB9'])) {$errorMessage .= "<h1>QB9 not specified</h1>";}
if (empty($_POST['QC1'])) {$errorMessage .= "<h1>QC1 not specified</h1>";}
if (empty($_POST['QD1'])) {$errorMessage .= "<h1>QD1 not specified</h1>";}
if (empty($_POST['QD2'])) {$errorMessage .= "<h1>QD2 not specified</h1>";}
if (empty($_POST['QD3'])) {$errorMessage .= "<h1>QD3 not specified</h1>";}
if (empty($_POST['QE1'])) {$errorMessage .= "<h1>QE1 not specified</h1>";}
if (empty($_POST['QE2'])) {$errorMessage .= "<h1>QE2 not specified</h1>";}
if (empty($_POST['QF1'])) {$errorMessage .= "<h1>QF1 not specified</h1>";}
if (empty($_POST['QF2'])) {$errorMessage .= "<h1>QF2 not specified</h1>";}
if (empty($_POST['QF3'])) {$errorMessage .= "<h1>QF3 not specified</h1>";}
if (empty($_POST['QF4'])) {$errorMessage .= "<h1>QF4 not specified</h1>";}
if (empty($_POST['QF5'])) {$errorMessage .= "<h1>QF5 not specified</h1>";}
if (empty($_POST['QG1'])) {$errorMessage .= "<h1>QG1 not specified</h1>";}
if (empty($_POST['QG2'])) {$errorMessage .= "<h1>QG2 not specified</h1>";}
if (empty($_POST['QG3'])) {$errorMessage .= "<h1>QG3 not specified</h1>";}
if (empty($_POST['QG4'])) {$errorMessage .= "<h1>QG4 not specified</h1>";}
if (empty($_POST['QH1'])) {$errorMessage .= "<h1>QH1 not specified</h1>";}
if (empty($_POST['QI1'])) {$errorMessage .= "<h1>QI1 not specified</h1>";}
if (empty($_POST['QI2'])) {$errorMessage .= "<h1>QI2 not specified</h1>";}
if (empty($_POST['QJ1'])) {$errorMessage .= "<h1>QJ1 not specified</h1>";}
if (empty($_POST['QJ2'])) {$errorMessage .= "<h1>QJ2 not specified</h1>";}
if (empty($_POST['QJ3'])) {$errorMessage .= "<h1>QJ3 not specified</h1>";}

4 个答案:

答案 0 :(得分:2)

这样的事情:

    // Required field names
    $required = array('QA1', 'QA2', 'QA3', .....);

    // Loop over field names, make sure each one exists and is not empty
    $error = false;
    foreach($required as $field) {
      if (empty($_POST[$field])) {
        $error = $field;
        break;
      }
    }

    if ($error) {
      echo "All fields are required." . $field;
    } else {
      echo "Proceed...";
    }

答案 1 :(得分:1)

这可能会对您有所帮助,但如果您不需要检查

,则应排除其他帖子变量
if(isset($_POST)) {
    foreach($_POST as $post) {
         if(empty($post)) {
             $errorMessage .= "<h1>".$post."not specified</h1>";
         }
    }

}

答案 2 :(得分:1)

在您的HTML表单中,尝试为输入命名,例如 my_form [QG2] my_form [QA1] ,... 所以在你的控制器中,你将把my_form作为一个数组来处理,如下所示,它将非常容易处理错误:

<?php
    $myforms = $_POST['my_form'];
    foreach ($myforms as $key => $mf) {
    if (empty($mf)) {$errorMessage .= "<h1>$key not specified</h1>";}
    }

答案 3 :(得分:-1)

考虑使用javascript进行表单验证。我用过validate.js并且不能抱怨 https://rickharrison.github.io/validate.js/