tags:

views:

72

answers:

3

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>

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.

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);
?> 
+1  A: 
$_POST['yr'] == checked 

should be:

$_POST['yr'] == 'on'

The default for firefox is 'on', maybe different in other browsers. (Thanks to David)

zaf
It shouldn't, the value of the input is not "checked". Since it isn't set, it will be whatever the browser defaults to ('On' IIRC, but its always better to set it explicitly)
David Dorward
Thanks for that. I'll edit my post.
zaf
+1  A: 

try the following:

if (isset($_POST['yr'])) { ... }
Elie
+3  A: 

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>
ZeissS
thanks, a lot..