在PHP表格中自动选中复选框

时间:2013-02-20 14:12:18

标签: php checkbox checked

我是PHP的新手,我有问题。我有一张带复选框的桌子。我需要在复选框中添加if语句。

  echo "<div class='table1'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
echo "<form action='confirmsubmission.php' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'  
value='".$row['module_id']."' />" . "</td>";
echo "<td>" . $row['module_id'] .  "</td>"; 
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></div>";

下面是我需要在value ='“。$ row ['module_id']之后添加的If语句。”'

 if($row['module_choice']=='Mandatory'){ echo "checked=\"true\""; }

2 个答案:

答案 0 :(得分:0)

即使我建议您使用模板系统(Smarty等),您也可以执行以下代码:

echo "<div class='table1'>
<form action='confirmsubmission.php' method='post'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'  
value='".$row['module_id']."'";
if($row['module_choice']=='Mandatory'){ 
    echo " checked='checked' "; 
}
echo "/>" . "</td>";
echo "<td>" . $row['module_id'] .  "</td>"; 
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";

echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></form></div>";

答案 1 :(得分:0)

有点满口,但试试:

...    
echo "<td>" . "<input type='checkbox' name='check[]' value='".$row['module_id']."'".($row['module_choice']=='Mandatory' ? 'checked="true"' : "")." />" . "</td>";
...