如何为每个文本输入编写$ _POST方法

时间:2012-10-09 22:22:43

标签: php javascript html

我在表行上有一堆,每行包含一堆文本输入。现在,下面是可能输入的名称属性,它们在每个表行中包含自己的值:

answerAvalue = A
answerBvalue = B
answerCvalue = C
answerDvalue = D
answerEvalue = E

... //all the way to answerZvalue = Z

answerTruevalue = True
answerFalsevalue = False
answerYesValue = Yes
answerNovalue = No

每个表格行还包含一些下面的单选按钮:

<input type="radio" name="reply" />= = Single
<input type="radio" name="reply" />= = Multiple

因此,假设我遍历每个表行并检索每行中选中的每个单选按钮的值,然后代码如下:

$i = 0;
$c = count($_POST['gridValues']); //counts number of appended rows

for($i = 0;  $i < $c; $i++ ){ //for each loop which goes through each row

switch ($_POST['reply'][$i]){ 

case "single": 
$selected_reply = "Single"; 
break; 

case "multiple": 
$selected_reply = "Multiple"; 
break; 

default: 
$selected_reply = ""; 
break; 

}

但我的问题是,因为每个文本输入都有自己的name属性,那么我如何编写代码来实现与上面相同的内容,但显然对于文本输入呢?

2 个答案:

答案 0 :(得分:3)

foreach($_POST as $key=>$value)
echo $key."=>".$value; //key is for example "reply" and value f.e. "2"

答案 1 :(得分:1)

foreach($_POST as $key=>$value)
{
  if(is_array($value))
  {
     foreach($value as $k => $v)
     {
       echo k."=>".$v; 
     }
  }
  else
  {
     echo $key."=>".$value; 
  }
}