当有人在文本框中输入一个月时,我试图使用switch语句,点击提交将在PHP中显示一条消息

Okay so i have created a switch statement and want to integrate a text box so when the user enters a month and hits submit a message will display for each month. what am i missing i think i am close to getting this complete:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Enter a month: <input type = "text" name ="month">
<input type = "submit">
</form>
<?php
switch($month){
case december:
    echo " its a chilly winter";
    break;
case january:
    echo " its a chilly winter";
    break;
case febraury:
    echo " its a chilly winter";
    break;
case march:
    echo " its a beautiful spring";
    break;
case april:
    echo " its a beautiful spring";
    break;
case may:
    echo " its a beautiful spring";
    break;
case june:
    echo " the heat of summer";
    break;
case july:
    echo " the heat of summer";
    break;
case august:
    echo " the heat of summer";
    break;
default: 
    echo "please enter a month.";
}
?>

Two issues that prevent it from working:

  • Quote the case values "december" or 'december'
  • The value from the form comes in the $_POST array as $_POST['month']

So for a start:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Enter a month: <input type="text"   name="month">
               <input type="submit" name="submit">
</form>
<?php
switch(strtolower($_POST['month'])){
    case "december":
    case "january":
    case "february":
        echo " its a chilly winter";
        break;
    case "march":
    case "april":
    case "may":
        echo " its a beautiful spring";
        break;
    case "june":
    case "july":
    case "august":
        echo " the heat of summer";
        break;
    default: 
        echo "please enter a month.";
}

Also, if you put them in order to "fall through" you don't need repetitive code in each of the cases. Here if you input december it will end up at february (since there are no break statements) and echo the same message.

You can also add strtolower() so that if DECEMBER is entered it will be converted and still match december.

You will probably need to add name="something" (I used submit) to the submit so that you can later check that the form has been submitted with:

if(isset($_POST['submit'])) {
    //do stuff
}

While developing/testing, make sure to display errors with this before any code:

error_reporting(E_ALL);
ini_set('display_errors', '1');

BTW, where is september, october, november???