-1

我正在将大量数据从表单提交到 php 脚本,我想要一种有效的方法来检查所有数据是否正确提交。

我的代码

<?php
$Name = $_POST['Name'];
$ID = $_POST['ID'];
$Submit = $_POST['submit'];
$Reset = $_POST['reset'];
$Topic_1 = $_POST['1'];
$Topic_2 = $_POST['2'];
$Topic_3 = $_POST['3'];
$Topic_4 = $_POST['4'];
$Topic_5 = $_POST['5'];
$Topic_6 = $_POST['6'];
$Topic_7 = $_POST['7'];
$Topic_8 = $_POST['8'];
$Topic_9 = $_POST['9'];
$Topic_10 = $_POST['10'];
$Topic_11 = $_POST['11'];
$Topic_12 = $_POST['12'];
$Topic_13 = $_POST['13'];
$Topic_14 = $_POST['14'];
$Topic_15 = $_POST['15'];
4

3 回答 3

5
$fields_names = array_merge(array(('Name','ID','submit')) , range(1,15)); /// instead of manually setting the numbered variables , used range function / @Fred-ii-

foreach($fields_names as $key)
{
 if(empty($_POST[$key]) && $_POST[$key] != 0) // Making sure it's not 0 / @deceze
 {
  $error = true;
  echo $key . ' is not set';
 }
}

if(!$error)
{
  //Anything is set.
}

注意 我不确定检查该值是否empty足够。考虑添加更多验证规则。

于 2013-09-02T18:30:45.127 回答
1

使用foreach循环:

if (!empty($_POST)) {
    foreach ($_POST as $value) {
        if (isset($value)) {
            # variable set
        }
    }
}
于 2013-09-02T18:32:24.740 回答
0

只需使用

foreach($_POST)
{
   //test
}

如果您还需要数组键,请查看 array_keys 的功能。您将获得每个键的数组。

应该这样做:

$array_is_empty = false
foreach (array_key($_POST) as $x){
    if(empty($_POST[$x]))
    $array_is_empty = true
}

抱歉没时间测试

问候马丁

于 2013-09-02T18:37:17.943 回答