如何检查一个复选框/单选按钮是否在PHP中被选中 [英] How to check if a checkbox/ radio button is checked in php

查看:86
本文介绍了如何检查一个复选框/单选按钮是否在PHP中被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个html代码:

I have this html code:

<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 ,然后根据检查的值输出相应的数据。

But I don't have any idea on how do I check this checkboxes if they are checked using php, and then output the corresponding data based on the values that are checked.

请帮忙,我在想这样的事情。但当然这是行不通的,因为我不知道如何将php中的复选框等同于checkbox:

Please help, I'm thinking of something like this. But of course it won't work, because I don't know how to equate checkboxes in php if they are checked:

<?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);
?> 


推荐答案

您必须为您的复选框赋予一个值。这个值被发送到服务器,以防复选框被选中。

You must give your checkboxes a value. This value gets send to the server, in case the checkbox is checked.

if ( $_POST['checkboxname'] == 'checkboxvalue' ) {

}

由于我看不到表单:
要将数据发送到服务器,需要输入元素的一个表单:

Since I see no form: To send the data to the server, you need a form around your input elements:

<form method="POST" action="myphpscript.php">
    YOUR CONTENT HERE
</form>

这篇关于如何检查一个复选框/单选按钮是否在PHP中被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆