PHP Forms Ex. #4: Switch Statement

You probably noticed that the if-elseif-else construction was repetitive and cumbersome in the last exercise. It works best with only one or two choices. A more appropriate construction for this exercise is the switch statement, combined with a select field in the form for the days of the week. So your assignment in this PHP exercise is to rewrite the previous exercise using a select field for the user input and the switch statement to process the response.

Once again, here's the poem:

Laugh on Monday, laugh for danger.
Laugh on Tuesday, kiss a stranger.
Laugh on Wednesday, laugh for a letter.
Laugh on Thursday, something better.
Laugh on Friday, laugh for sorrow.
Laugh on Saturday, joy tomorrow.

Remember to include a general response for any input that is not in the poem. To make things a little more interesting, include a 'Back' button on the response so that the user can go back and try different days.



Here's the script:

  1. <!DOCTYPE html 
  2.      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3.      "">
  5. <html xmlns=""  xml:lang="en" lang="en">
  6. <head>
  7. <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
  8. <title>Switch Pick a Day</title>
  9. </head>
  11. <body bgcolor="#ffffff" text = "#000000">
  12. <h2>Pick a Day</h2>
  13. <?php
  14. //If form not submitted, show form.
  15. if (!isset($_POST['submit'])){
  16. ?>
  18. <form method="post" action="yourfile.php">
  19. Please choose a day:<p />
  20. <select name="day">
  21.    <option value="Monday">Monday</option>
  22.    <option value="Tuesday">Tuesday</option>
  23.    <option value="Wednesday">Wednesday</option>
  24.    <option value="Thursday">Thursday</option>
  25.    <option value="Friday">Friday</option>
  26.    <option value="Saturday">Saturday</option>
  27.    <option value="Sunday">Sunday</option>
  28. </select> 
  29. <p />
  30. <input type="submit" name="submit" value="Go"/>
  31. </form>
  33. <?php
  34. //If form submitted, process input.
  35. }else{
  36. $day = $_POST['day'];
  37. switch($day){
  38.   case 'Monday':
  39.     echo 'Laugh on Monday, laugh for danger.';
  40.     break;
  41.     /*Did you remember your break statements for each option? 
  42.     If not, once started, the script will continue to execute
  43.     the instructions to the end of the switch statement.*/
  44.   case 'Tuesday':
  45.     echo 'Laugh on Tuesday, kiss a stranger.';
  46.     break;
  47.   case 'Wednesday':
  48.     echo 'Laugh on Wednesday, laugh for a letter.';
  49.     break;
  50.   case 'Thursday':
  51.     echo 'Laugh on Thursday, something better.';
  52.     break;
  53.   case 'Friday':
  54.     echo 'Laugh on Friday, laugh for sorrow.';
  55.     break;
  56.   case 'Saturday':
  57.     echo 'Laugh on Saturday, joy tomorrow.';
  58.     break;
  59.   default:
  60.     echo 'No information for that day.';
  61.     break; 
  62. }
  63. ?>
  64. <p />
  65. <form action="yourfile.php">
  66. <input type="submit" name="submit" value="Back" onclick="self.location='yourfile.php'" />
  67. </form>
  68. <?php
  69. }
  70. ?>
  71. </body>
  72. </html>

See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.


I copied and pasted the entire script for the switch statement exercise and uploaded in my server. The form shows up correctly but when I click the go button an error page comes up saying this page is not available or never been created. I am using hostgator web hosting for my site and I would like to know, do I have to set up anything before proceeding to these exercise. The other exercises worked fine though, only this form exercise is causing problem.
Any help will be hugely appreciated.

Anywhere in the script that says: yourfile.php you need to replace with your file name.php. If not, it is not going to recognize the file name.

here is my complete code for this exercise. with a simple but effective string conversion and filtering system for smooth code flow.

  1. <form action="" method="post">
  2.   Enter a week day:
  3.   <input name="dayname" type="text" maxlength="100" />
  4.   <input type="submit" value="Check it!">
  5. </form>
  7. <?php
  8.   if (isset($_POST["dayname"]) && !empty($_POST["dayname"])){
  9.     $common="Laugh on";
  10.     $users=ucfirst(strtolower($_POST["dayname"]));
  11.     switch ($users) {
  12.     case 'Monday': 
  13.       echo "$common $users, laugh for danger.";
  14.       break;
  15.     case 'Tuesday': 
  16.       echo "$common $users, kiss a stranger.";
  17.       break;
  18.     case 'Wednesday': 
  19.       echo "$common $users, laugh for letter.";
  20.       break;
  21.     case 'Thursday': 
  22.       echo "$common $users, something better.";
  23.       break;
  24.     case 'Friday': 
  25.       echo "$common $users, laugh for sorrow.";
  26.       break;
  27.     case 'Saturday': 
  28.       echo "$common $users, joy tomorrow.";
  29.       break;
  30.     default: 
  31.       echo "Its rest day. But you can try again. =)";
  32.     }
  33.   }
  34.   else echo " ";	
  35. ?>

Just wanted to point out that Javascript is used for the button. What if javascript is disabled?

I was wondering if server-side solutions are feasible, e.g. using REFERRER?