PHP遍历表单值 [英] PHP Loop through form values
本文介绍了PHP遍历表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单,其中提交了许多小字段,我需要遍历这些小字段并对其执行操作.
I have a form that submits lots of small fields that I need to loop through and perform actions on.
表单如下:
<input class="" type="hidden" name="pid" value="10">
<input class="" type="hidden" name="date" value="01-01-2014">
然后大约有100-200 ...
Then around 100 - 200 of these...
<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"中,将它们都存储在vars中,最后执行数据库查询,然后再转到下一个查询.
When the form is submitted, I need to loop through each "minnight" field, extract the "12_2014-03-06" into "12" and "2014-03-06", store them both in vars and finally do a database query before moving to the next one.
任何有关我应该从这个烂摊子开始的指针吗?
Any pointers on where I should start with this mess?
推荐答案
使用 POST变量
尝试以下方法:
// 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.
}
}
这篇关于PHP遍历表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文