Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

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.

 

Comments

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.
Thanks.

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>
  6.  
  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?