在while循环中获取值

cwdobuhd  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(366)

当name属性在while循环中时,有什么方法可以获取它吗?我想调用单选按钮的name属性来插入数据库

if ($result = $conn - > query($query)) {
    while ($row = $result - > fetch_row()) {
        //$app_rows.append($row[1])
        $app_rows = array($row[1]);
        ?>
        < tr >
            < td id = "app_name" > <? php foreach($app_rows as $values) {
                echo $values; ?> < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P0 "; ?>"
                value = "P0" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P1 "; ?>"
                value = "P1" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P2 "; ?>"
                value = "P2" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P3 "; ?>"
                value = "P3" > < /td> < td > < input type = "radio"
                name = "<?php echo $values; ?>"
                id = "<?php echo $values. " - P4 "; ?>"
                value = "P4" > < /td> <? php
            } ?>
            < /tr> <? php
    }
    $result - > close();
}
eh57zj3b

eh57zj3b1#

if($result = $conn->query($query))
{
    while($row = $result->fetch_row())
    {
        $app_rows = array($row[1]);
        echo"<tr>";
        foreach($app_rows as $values)
        {
            echo"<td id='app_name'>".$values."</td>";
            for($c=0;$c<=4;$c++)
            {
                echo"<td> <input type='radio' name='".$values."' id='".$values.'-'.$c."' value='P".$c."''></td>";
            }
        }
        echo"</tr>";
    }
}
wwodge7n

wwodge7n2#

可以在属性名中使用数组,例如在代码中

<td> <input type="radio" name="radioVal[]" id="<?php echo $values. "-P0"; ?>" value="P0"></td>
<td> <input type="radio" name="radioVal[]" id="<?php echo $values. "-P1"; ?>" value="P1"></td>
<td> <input type="radio" name="radioVal[]" id="<?php echo $values. "-P2"; ?>" value="P2"></td>

然后在后端

$radioVal = $_POST['radioVal'];
if(count($radioVal)!=0){
$radioIsVal = '';
for ($i=0; $i < count($radioVal); $i++) { 
 $radioIsVal .= $radioVal[$i].',';
}
$radioIsVal = rtrim($radioIsVal ,',');
// $radioIsVal the value is P0, P1, P2
}else{
echo 'no radio value';
}

愿它能帮助你

相关问题