【问题标题】:Get the hidden input of radio button获取单选按钮的隐藏输入
【发布时间】:2018-02-07 14:40:06
【问题描述】:

我有一个单选按钮,它使用包含字符串值的 foreach 循环从数据库中获取和显示。我设置了另一个隐藏输入以及保存每个数据 id 的单选按钮。

$sql = "SELECT DISTINCT(class) FROM program WHERE level = '".$level."' AND title = '".$title."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $i = 1;
    while($row = $result->fetch_assoc()) {
        echo '<ul>';
        echo '<li>';
        echo $row['class'];
        echo '<input type="hidden" value="'.$row['class'].'"';
        echo '</li>';
        echo '<li>';
        echo '<ul><li><input name="timeslot['.$i.']" type="radio" value="" checked>N/A</li></ul>';

        $sql2 = 'SELECT * FROM program WHERE class = "'.$row['class'].'"';
        $result2 = $conn->query($sql2);

        if ($result->num_rows > 0) {
            while($row2 = $result2->fetch_assoc()) {
                echo '<ul>';
                echo '<li>';
                echo '<input name="timeslot['.$i.']" type="radio" value="'.$row['class'].', '.$row2['timeslot'].'" />'.$row2['timeslot'];
                echo '<input type="hidden" name="slot_id['.$i.']" value="'.$row2['id'].'"';
                echo '</li>';
                echo '</ul>';
            }
        }

        echo '</li>';
        echo '</ul>';
        $i++;
    }
}

提交表单时,我的隐藏值总是获取每个组的最后一行。如何在选中的单选按钮上获取 id 值?

【问题讨论】:

标签: php foreach radio-button hidden-field


【解决方案1】:

你没有关闭输入隐藏字段。

另外,如果您不需要服务器中的每个 id,我建议您更改这种方式而不是 radio 和 hidden。

echo '<input name="timeslot['.$row2['id'].']" type="radio" value="'.$row['class'].', '.$row2['timeslot'].'" />'.$row2['timeslot'];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多