形式:foreach在一个循环中

时间:2013-07-18 07:18:16

标签: php forms loops foreach

我正在使用foreach并遇到一些问题。我有2个循环,通常是随机值(这里我把修复值)。我想使用foreach从表单中获取值

为了更好地解释它,表单看起来像这样:

Number 1 : T1 : ----
Number 2 : T2 : ---- and -----
So the user enter for example 2, 3, 4
So
Number 1 : T1 : 2
Number 2 : T2 : 3 and 4

我得到的结果只有3和4.值2永远不会保存。 我认为问题是因为循环将再次启动foreach,因此之前输入的值将被删除。但我不知道该怎么做。

这是我的代码:

<?php
session_start();
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
<?php 
for ($i = 1; $i <= 2; $i++) {
    echo "Numero ";
    echo $i;
    for ($j = 1; $j <= 3; $j++) {
        echo $j;
        echo "<input type='text' name='number3[$j]' id='number3{$j}' />";
    }
}   
?>
<input type="submit" name="submitbutton" value="Confirm!">
</form>

<?php
if (isset($_POST['submitbutton'])) {

    for ($i = 1; $i <= 2; $i++) {
    echo "Numero ";
    echo $i;
    for ($j = 1; $j <= 3; $j++) {
                echo "Lettre ";
                echo $j;
        foreach ($_POST['number3'] as $key => $value) {
            $_SESSION["number3_{$key}"] = $value;
            echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
        }   

}   
}
    }
?>

2 个答案:

答案 0 :(得分:0)

首先,我不太了解您要完成的任务,但您的代码中存在问题。这是您的脚本生成的html标记:

<form action="/so.php" method="post"> 
Numero 1
1<input type='text' name='number3[1]' id='number31' />
2<input type='text' name='number3[2]' id='number32' />
3<input type='text' name='number3[3]' id='number33' />

Numero 2
1<input type='text' name='number3[1]' id='number31' />
2<input type='text' name='number3[2]' id='number32' />
3<input type='text' name='number3[3]' id='number33' />

<input type="submit" name="submitbutton" value="Confirm!">
</form>

如您所见,第二组输入会覆盖前一个输入,因为您的名称与number3[1]相同。

您应该使用多维数组使您的HTML看起来像

<input type='text' name='number3[1][1]' id='number31' />

脚本中的迭代部分就像

if (isset($_POST['submitbutton'])) {
    foreach ($_POST['number3'] as $firstKey => $insideArray) {
    echo "Numero ";
    echo $firstKey;
        foreach ($insideArray as $secondKey => $value) {
            $sessionKey = $firstKey . '_' . $secondKey;
            echo "Lettre ";
            echo $secondKey;
            $_SESSION[$sessionKey] = $value;
            echo 'The value of $_SESSION['.$sessionKey.'] is '."'".$value."'".' <br />';
        }
    }
}

答案 1 :(得分:0)

据我所知,PHP允许您发布多维变量。你的代码看起来会像这样(我没有尝试过 - 所以它只是“一个”片段。)

表单的生成(通常是动态的):

$numbers=2;
//note: $numbers is normally random (here 2 number records are generated)
for ($i = 1; $i <= $numbers; $i++) {
    echo "Number "+ $i + " <br/>";

    $fields=3;
    //note: $fields is normally random (here three fields per number are generated)
    for ($j = 1; $j <= $fields; $j++) {
        //this names the field by number[number_counter][field_counter]
        echo "<input type='text' label="+ $j +" name='number["+ $i +"]["+ $j +"]' />";
    }
}

提取已发布的表单变量:

if (isset($_POST['submitbutton'])) {

    $current_number=1;
    foreach ($_POST['number'] as $number)
    {
        echo "Number " + $current_number +":<br/>";
        $field_counter = 1;
        foreach ($number as $field)
        {
            echo " The value of field #"+ $field_counter +"is "
              + $number[$field_counter] +".<br/>";
        }
    }

}