在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();
}
答案 0 :(得分:1)
您可以在属性名称中使用数组, 例如在您的代码中
<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';
}
也许可以帮助您
答案 1 :(得分:0)
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>";
}
}