我有这个HTML代码:
<tr>
<td><label><input type="text" name="id" class="DEPENDS ON info BEING student" id="example">ID</label></td>
</tr>
<tr>
<td>
<label> <input type="checkbox" name="yr" class="DEPENDS ON info BEING student"> Year</label>
</td>
</tr>
但是我不知道如果使用php检查它们如何选中此复选框,然后根据检查的值输出相应的数据。
请帮助,我在考虑这样的事情。但当然它不起作用,因为我不知道如果检查它们如何在php中等同于复选框:
<?php
$con = mysql_connect("localhost","root","nitoryolai123$%^");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("school", $con);
$id = mysql_real_escape_string($_POST['idnum']);
if($_POST['id'] == checked & $_POST['yr'] ==checked ){
$result2 = mysql_query("SELECT * FROM student WHERE IDNO='$id'");
echo "<table border='1'>
<tr>
<th>IDNO</th>
<th>YEAR</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['YEAR'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($con);
?>
答案 0 :(得分:3)
您必须为复选框添加值。如果选中该复选框,则会将此值发送到服务器。
if ( $_POST['checkboxname'] == 'checkboxvalue' ) {
}
因为我看不到任何形式: 要将数据发送到服务器,您需要一个围绕输入元素的表单:
<form method="POST" action="myphpscript.php">
YOUR CONTENT HERE
</form>
答案 1 :(得分:1)
$_POST['yr'] == checked
应该是:
$ _POST['yr'] == 'on'
firefox的默认值为“on”,在其他浏览器中可能有所不同。 (感谢大卫)
答案 2 :(得分:1)
尝试以下方法:
if (isset($_POST['yr'])) { ... }
答案 3 :(得分:0)
如果您包含一个隐藏字段,具有您希望在帖子数据中显示的相同名称和失败值,那么当复选框未返回值(未选中)时,表单上的隐藏控件将
echo '<form method="post"><input type="hidden" name="checkdata" value="0">\
<input type="checkbox" name="checkdata" value="1">\
<input name="submitbutton" type="submit"></form>\
</body></html>';
if ($_POST['submitbutton']) {
echo "Value:|".$_POST['checkdata']."|";
}