Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对于表单验证的每个循环_Php_Html_Forms_Validation_Foreach - Fatal编程技术网

Php 对于表单验证的每个循环

Php 对于表单验证的每个循环,php,html,forms,validation,foreach,Php,Html,Forms,Validation,Foreach,好的,我要对一个有很多行(20+)和每行4个字段的表单进行一些验证 每列都有特定的验证要求 行应该是完整的,而不是半满的。应跳过空行 如果html表单中的每个字段都有一个唯一的名称,如何遍历表单。例如productCode_1、productCode_2、productCode_3等 <tr> <td><div align="center"> <input name="productCode_1" type="text" id="product

好的,我要对一个有很多行(20+)和每行4个字段的表单进行一些验证

  • 每列都有特定的验证要求
  • 行应该是完整的,而不是半满的。应跳过空行
如果html表单中的每个字段都有一个唯一的名称,如何遍历表单。例如productCode_1、productCode_2、productCode_3等

 <tr>
<td><div align="center">
  <input name="productCode_1" type="text" id="productCode_1" size="7" maxlength="7" />
</div></td>
<td><div align="center">
  <input name="size_1" type="text" id="size_1" size="2" maxlength="2" />
</div></td>
<td><div align="center">
    <input name="quantity_1" type="text" id="quantity_1" size="3" maxlength="3" />
</div></td>
<td><div align="center">
    <input name="price_1" type="text" id="price_1" size="3" maxlength="3" />
</div></td>


上面是HTML的一个示例,请注意,我没有修改HTML的权限。

您可以将要验证的字段的字段名放在数组中,然后它将遍历这些字段。我已将一个简单的
空的
检查,但您将添加自己的复杂检查

    // All the names of the fields you wish to validate.
    $myFieldNames = [];
    $hasErrorOccurred = false;

    foreach ($myFieldNames as $name) {
        if (empty($_POST[$name])) {
            $hasErrorOccurred = true;

            break;
        }
    }

    if ($hasErrorOccurred) {
        // Your error code here.
    } else {
        // Your successful code here.
    }
阅读材料:


我假设如果您有20行显示的HTML,那么name=“..”名称将类似于
productCode\u 1
productCode\u 2

$fields=array('productCode','size','quantity','price');
$msg=array();//查看错误消息
$max_rows=?;//要检查多少行,无法看到足够的代码来为您计算出这一行
$ok=true;
对于($i=1;$i<$max_行;$i++){
foreach($fields作为$field){
如果(!isset($field.$i))
$ok=false;
$msgs[]=“第$i行的$field中缺少数据”;
//continue是可选的,如果set将停止处理行
//当发现第一个错误时,
//没有它,您可以报告每行的所有错误
继续;
}
}
如果($ok){
//完整表单处理
}否则{
//报告所有错误
}
$fields = array('productCode_', 'size_', 'quantity_', 'price_');
$msg = array();  // for error messages
$max_rows = ?; // how many rows to check, cannot see enough of your code to work this one out for you


$ok = true;
for ( $i=1; $i < $max_rows; $i++ ) {

    foreach ( $fields as $field ) {
        if ( ! isset( $field . $i ) )
            $ok = false;
            $msgs[] = "Missing data in $field on row $i";
            // continue is optional, if set will stop processing a row
            // when first error is found, 
            // without it you can report all errors on each row
            continue;   
    }

}

if ( $ok ) {
    // complete form processing
} else {

    // report back all errors
}