Categories
Ask Development Geography

How to use check boxes with if statements in php

Hie guys. Please help, i want to output text if a specific combination of two or more check boxes are checked using if statements or even a switch case in php. For example if the user checks Maths and Geography, the program will say “You qualify for a Honors Degree in wildlife management” Its more of a mini university counselling system. The problem is i can’t figure out how to use the “if” statement for individual check boxes.

    <!DOCTYPE HTML PUBLIC>
<html>
<head>
    <title>checkbox</title>
</head>

<body>


<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <p>
        What subjects did you pass at A 'Level?<br/>
        <input type="checkbox" name="subject[]" value="A" />Maths<br />
        <input type="checkbox" name="subject[]" value="B" />English<br />
        <input type="checkbox" name="subject[]" value="C" />Science<br />
        <input type="checkbox" name="subject[]" value="D" />Religious Education<br />
        <input type="checkbox" name="subject[]" value="E" />Geography
    </p>

    <input type="submit" name="formSubmit" value="Submit" />
</form>


<?php
  $aSub = $_POST['subject'];
  if(empty($aSub))
  {
    echo("You didn't select any subjects.");
  }
  else
  {
    $N = count($aSub);

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


</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *