PHP循环表单值

时间:2014-01-16 16:58:35

标签: php forms loops foreach

我有一个表单,提交了许多我需要循环并执行操作的小字段。

表格如下:

<input class="" type="hidden" name="pid" value="10">
<input class="" type="hidden" name="date" value="01-01-2014">

然后大约100 - 200这些......

<input class="minnight" type="text" name="minnight_12_2014-03-06" value="2" size="1">
<input class="minnight" type="text" name="minnight_12_2014-03-07" value="2" size="1">
<input class="minnight" type="text" name="minnight_12_2014-03-08" value="1" size="1">
<input class="minnight" type="text" name="minnight_13_2014-03-06" value="3" size="1">
<input class="minnight" type="text" name="minnight_13_2014-03-07" value="2" size="1">
<input class="minnight" type="text" name="minnight_13_2014-03-08" value="4" size="1">
<input class="minnight" type="text" name="minnight_14_2014-03-06" value="1" size="1">
<input class="minnight" type="text" name="minnight_14_2014-03-07" value="2" size="1">
<input class="minnight" type="text" name="minnight_14_2014-03-08" value="2" size="1">

提交表单时,我需要遍历每个“minnight”字段,将“12_2014-03-06”解压缩为“12”和“2014-03-06”,将它们存储在变量中,最后执行移动到下一个数据库之前的数据库查询。

关于我应该从这个烂摊子开始的地方的任何指示?

3 个答案:

答案 0 :(得分:5)

查看explode()POST variables的使用情况。

尝试以下方面的内容:

// Loop over each item in the form.
foreach($_POST as $name => $value) {
    // Split the name into an array on each underscore.
    $splitString = explode("_", $name);

    // If the data begins with "minnight", use it.
    if ($splitString[0] == "minnight") {
        // Set the other desired values into variables.
        $secondValue = $splitString[1];
        $thirdValue= $splitString[2];

        // Database query goes here.
    }
}

答案 1 :(得分:0)

您可以遍历$ _POST并阅读值:http://www.php.net/manual/en/reserved.variables.post.php

foreach($_POST as $key =>$value){
    // do something using key and value
}

答案 2 :(得分:0)