【问题标题】:Multiple select in an for loop is losing it's data when returning to the form返回表单时,for循环中的多项选择正在丢失其数据
【发布时间】:2011-10-29 12:14:10
【问题描述】:

我在 while 循环中有一个带有多项选择的表单:

while ($row_i = mysql_fetch_array($res_i))
{
    $i++;

    // maak select name
    $name_bewerking_id = 'bewerking_id'.$i;
    ?>

    <tr valign="top">
        <td>
        <select name="<?php echo $name_bewerking_id ?>[]" multiple="multiple" size="2">
        <?php
            $sql = "SELECT id, bewerking FROM bewerkingen ORDER BY bewerking ASC";
            $res = mysql_query($sql,$con);
            while ($row = mysql_fetch_assoc($res))
            { ?>
                <option value="<?php echo $row['id']; ?>"><?php echo $row['bewerking']; ?></option>
        <?php } ?>
        </select>
        </td>
    </tr>
<?php
}

发送表单时:

$bewerking_id[$i] = array();
$bewerking_id[$i] = $_POST['name_bewerking_id'][$i];

if(isset($bewerking_id_temp[$i]))
{
    foreach($bewerking_id_temp[$i] as $temp[$i])
    {
        array_push($bewerking_id[$i], $temp[$i]);
    }
}

返回表格:

for ($i = 0; $i <= $aantal_regels_corr; $i++)
{
    // maak select name
    $name_bewerking_id = 'bewerking_id'.$i;
    ?>

    <tr valign="top">
        <td>
        <select name="<?php echo $name_bewerking_id ?>[]" multiple="multiple" size="2">
        <?php
            $sql = "SELECT id, bewerking FROM bewerkingen ORDER BY bewerking ASC";
            $res = mysql_query($sql,$con);
            while ($row = mysql_fetch_assoc($res))
            { ?>
                <option <?php if(isset($bewerking_id[$i]) && in_array($row['id'], $bewerking_id[$i])){ echo 'selected="selected"'; } ?> value="<?php echo $row['id']; ?>"><?php echo $row['bewerking']; ?></option>
        <?php } ?>
        </select>
        </td>
    </tr>
<?php
}

返回表单时(未填写其他字段之一时)所选选项将丢失且不会再次选择。

我在哪里搞砸了?

【问题讨论】:

    标签: php arrays multiple-select


    【解决方案1】:

    您使用此标识符 'name_bewerking_id' 读取 POST 数据

    但选择名称由&lt;?php echo $name_bewerking_id ?&gt;$name_bewerking_id = 'bewerking_id'.$i 给出

    【讨论】:

    • 而且您还在选择名称的末尾添加了“[]”...为什么?
    • 我添加了 [] 因为它是一个数组,没有 [] 的结果相同。我已经更改了标识符。没用
    【解决方案2】:

    如果假设您在 3 个不同页面之间传递表单信息,则需要为此使用 $_SESSION 变量。普通的 PHP 变量不能在页面之间传递,只能传递 $_SESSION 变量。

    例如:

    第 1 页

    <form ...>
        <input name="text1" type="text" />
        <input type="submit" />
    </form>
    

    第 2 页

    // must start the session before session variables can be used
    start_session();
    
    $inputTextBox1 = $_SESSION["textBox1"] = $_POST["text1"];
    

    第 3 页

    <?php start_session(); ?>
    <html>
        ...
    
        <form ...>
            <select>
                <?php while ... { ?>
                    <option <?php if(!empty($_SESSION["textBox1"])) { echo "selected=\"selected\""; } ?>>Some Text</option>
                <?php } // End while ?>
            </select>
        </form>
    
        ...
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多