Sunday, November 23, 2014

Checkbox handling in PhP for entering absentees

The interface that i showed earlier is quite rudimentary and we need something more sophisticated. Something like making a student absent on any day any hour using a single interface. So i experimented with a checkbox input.

We separated out the database access credentials and wrote a config.php file
---------------------------------
<?php
 $username="root";
$password="cec123";
$database="cec";
 ?>

---------------------------------

Then we wrote a simple1.html file which takes in details like semester number branch and section.
Looks like this:
-----------------------------------------------------------------------------------------------
<html>
<form name="formName" action="accessdbase2.php">
<input type="radio" name="sem" value="1">1
<input type="radio" name="sem" value="2">2
<input type="radio" name="sem" value="3">3
<input type="radio" name="sem" value="4">4
<input type="radio" name="sem" value="5">5
<input type="radio" name="sem" value="6">6
<input type="radio" name="sem" value="7">7
<input type="radio" name="sem" value="8">8
<select name="dept">
  <option value="cs">cs</option>
  <option value="ec">ec</option>
  <option value="is">is</option>
  <option value="me">me</option>
  <option value="cv">cv</option>
</select>
<input type="radio" name="sec" value="a">a
<input type="radio" name="sec" value="b">b
<input type="submit" value="Submit">
</form>
</html>
 ----------------------------------------------------------------------------------
This html page submits to accessdbase2.php
-----------------------------------------------------------------------------------
<html>

<h1>Hello!</h1>
<form name="formName" action="accessdbase3.php" method="post" style="width:800px;">
<table border="1">
<tr><td>USN-Student Name</td><td>1</td><td>2</td><tr>
<?php
ob_start();
require("config.php");
ob_end_clean();

$req=$_REQUEST['sem'];
$req2=$_REQUEST['dept'];
$req3=$_REQUEST['sec'];
$con=mysqli_connect("localhost",$username,$password,$database);

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query="SELECT * FROM students WHERE sem='$req' AND dept='$req2' AND sec='$req3'";

$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result))
  {
  echo "<tr><td> ";echo"{$row['USN']}-{$row['student_name']} </td> <td><input name='attend1[]' type='checkbox' checked value='{$row['USN']}'></td><td><input name='attend2[]' type='checkbox' value='{$row['USN']}'></td></tr>";
  }

mysqli_close($con);
?>

</table>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</html>
 ----------------------------------------------------------------------------------------------

This accessdbase2.php looks like this:
 It Submits to the accessdbase3.php which looks like this:

----------------------------------------------------------------------------------------------
Here is accessdbase3.php code:(Which illustrates handling checkboxes)
----------------------------------------------------------------------------------------------
 <?php
ob_start();
require("config.php");
ob_end_clean();
$absentees = $_POST['attend1'];
  if(empty($absentees))
  {
    echo("You didn't select any absentees.");
  }
  else
  {
    $N = count($absentees);

    echo("You selected $N absentee(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($absentees[$i] . " ");
    }
  }
?>
-------------------------------------------------------------------------------------------------

Thanks... your best wishes are with us.

No comments:

Post a Comment