0

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>
  • 1
    Possible duplicate of [How to read if a checkbox is checked in PHP?](https://stackoverflow.com/questions/4554758/how-to-read-if-a-checkbox-is-checked-in-php) – ricky3350 Oct 06 '19 at 04:10

3 Answers3

1

you are close :-)

Here is the PHP code updated with comments:

if (isset($_POST['subject'])) // This check is added, so you don't check an unset variable on load of page and get a warning.
{
  $aSub = $_POST['subject'];
  if(empty($aSub))
  {
    echo("You haven't selected any subjects.");
  }
  else
  {
    $N = count($aSub);

    echo("You selected $N subject(s): ");

  }

  //array of subjects is imploded into string

  $chosen_subjects = implode ("", $aSub);

  // use print_r to print content of either string or array/object

  // print_r($aSub);
  // print_r($chosen_subjects);

  // This is the result of the above 2 print_r
  // Array
  // (
  //     [0] => Agriculture
  //     [1] => Biology
  // )
  // AgricultureBiology  

  // if ($chosen_subjects == 'Biology' && 'Agriculture' )
  /**
   * If you wanted to test for those two variables "Biology" and "Argriculture" then you have to do like this:
   * if ($aSub[0] == 'Agriculture' && $aSub[1] == 'Biology')
   * By using implode, we are converting the array of options into a string.
   */

   if ($chosen_subjects == 'AgricultureBiology' )
  {
      echo "You qualify for a Bachelor of Agricultural Sciences Honors Degree in Animal Science and Rangeland Management";
  }
}

Be aware that this approach may not be the easiest. You now have 14 options which is 43589145600 possible combinations (14! / 2) That's a lot :-)

A suggestion. How many bachelor degrees do you have? Let's say you have 5. If you showed those, and then made it possible for the user to click that box, which then expanded and then showed which subjects were need with which grades it would be a lot simpler to present and understand.

stubben
  • 309
  • 2
  • 6
0

To simplify options, you can merge subjects into one string and check that. There are a lot of options (60 (5x4x3x2x1 / 2) which will be a lot of if statements :-)

A suggestion is for you to replace your for loop with this logic and expand from there:

  // Array of subjects is imploded into a string
  $chosen_subjects = implode("", $aSub);
  if ($chosen_subjects == 'AE')
  {
    echo('You qualify for a Honors Degree in wildlife management');
  }
stubben
  • 309
  • 2
  • 6
  • Thank you, i have used the logic and i keep getting an error which says "Warning: implode(): Invalid arguments passed in C:\Users\Otis Mabikwa\Desktop\test.php on line 52" and after trying to check for two conditions using an if statement, the if statement disregarded the second condition and only checked the first if ($chosen_subjects == 'Biology' && 'Agriculture' ) the echo is only executed when Biology is true, even when agriculture is false – Otis Mabikwa Oct 08 '19 at 00:02
0
<!DOCTYPE HTML PUBLIC>
<html>
<head>
    <title>PHP form check box example</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="Accounts" />Accounts<br />
    <input type="checkbox" name="subject[]" value="Agriculture" />Agriculture<br />
    <input type="checkbox" name="subject[]" value="Biology" />Biology<br />
    <input type="checkbox" name="subject[]" value="Business_studies" />Business Studies<br />
    <input type="checkbox" name="subject[]" value="Chemistry" />Chemistry<br />
    <input type="checkbox" name="subject[]" value="Computer_Science" />Computer Science<br />
    <input type="checkbox" name="subject[]" value="Economics" />Economics<br />
    <input type="checkbox" name="subject[]" value="English" />English<br />
    <input type="checkbox" name="subject[]" value="Food_Science" />Food Science<br />
    <input type="checkbox" name="subject[]" value="Geography" />Geography<br />
    <input type="checkbox" name="subject[]" value="Maths" />Maths<br />
    <input type="checkbox" name="subject[]" value="Physics" />Physics<br />
    <input type="checkbox" name="subject[]" value="Religious Education" />Religious Education<br />
    <input type="checkbox" name="subject[]" value="Science" />Science<br />

        </p>

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


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

    echo("You selected $N subject(s): ");

  }

//array of subjects is imploded into string
$chosen_subjects = implode ("", $aSub);
if ($chosen_subjects == 'Biology' && 'Agriculture' )
{
    echo "You qualify for a Bachelor of Agricultural Sciences Honors Degree in Animal Science and Rangeland Management";
}

?>


</body>
</html>

@stubben