PHP Switch Statement – output result from selected option


I was asked to come up with a way to give users a list to choose from and output a result based on their selection. I decided to use PHP. I ended up using a Switch Statement to accomplish this task. I am not a PHP expert  but was happy to figure this out on my own.

Create the form

Create choice.html

<form action="choice.php" method="post">
<b>Choose your Selection:</b><br /><br />
 <input type="radio" name="formChoices[]" value="A" />A<br />
 <input type="radio" name="formChoices[]" value="B" />B<br />
 <input type="radio" name="formChoices[]" value="C" />C<br />
 <input type="radio" name="formChoices[]" value="D" />D<br />
 <br /><input type="submit" name="formSubmit" value="Submit" />
</form>

Output should look like this.

choice

Create the PHP action (called choice.php)

<?php

$result = $_POST['formChoices'];

switch ($result[0])
{
 case "A":
 echo "You have selected A.";
 break;
 case "B":
 echo "You have selected B.";
 break;
 case "C":
 echo "You have selected C.";
 break;
 case "D":
 echo "You have selected D.";
 break;
}

?>
,