0

我正在使用 foreach 并遇到一些问题。我有 2 个具有通常随机值的 for 循环(这里我放了固定值)。我想使用 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 />';
        }   

}   
}
    }
?>
4

2 回答 2

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/>";
        }
    }

}
于 2013-07-18T08:01:38.590 回答
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 />';
        }
    }
}
于 2013-07-18T07:55:05.367 回答