How can I create a drop down list that after choosing one in the list and clicking submit, the accepting page should use switch statement to check the corresponding value of the option in the list...

for example, the code below is a switch statement, than when my input is Jehzeel1, the page shows jehzeel likes apples, and so on...

<?php

$X = input;
switch ($X)  { 

case "Jehzeel1":
 echo "Jehzeel likes apples";
break;

case "Jehzeel2":
 echo "Jehzeel likes bananas";
break;

case "Jehzeel3":
 echo "Jehzeel likes oranges";
break;

default:
  echo "Input did not match with any case";

  }
?>

My main question is, how can I make a drop down list that its input will be used in the switch statement...

my options are

  1. Jehzeel1
  2. Jehzeel2
  3. Jehzeel3

if I select Jehzeel1, the page will then show, Jehzeel likes apples...

This is my drop down menu...

<form name="input">
<select name="dropdown">
<option value="Jehzeel1">Jehzeel1</option>
<option value="Jehzeel1">Jehzeel1</option>
<option value="Jehzeel1">Jehzeel1</option>
</select>
</form>

but it doesn't work, is there something wrong with my code? or something else that I forget to define..? will i change the form name? or is it ryt? nid help.. im a very newbie on this stuff 🙁

    Get rid of your $X = input; line. Then replace switch ($X) with switch ($_POST['dropdown']) ...it should work then. Hope this helps.

      thanks for the reply Merve... ___ i'll try it right away.. 😉

        This is my full code, i did the $POST method as what you've said, but it still doesn't work...:

        <?php
        
        
        echo "<form name="input">";
        echo"<select name="dropdown">";
        echo"<option value="Jehzeel1">Jehzeel1</option>";
        echo"<option value="Jehzeel2">Jehzeel2</option>";
        echo"<option value="Jehzeel3">Jehzeel3</option>";
        echo"</select>";
        echo"</form>";
        
        switch ($_POST['dropdown'])  { 
        
        case "Jehzeel1":
         echo "Jehzeel likes apples";
        break;
        
        case "Jehzeel2":
         echo "Jehzeel likes bananas";
        break;
        
        case "Jehzeel3":
         echo "Jehzeel likes oranges";
        break;
        
        default:
          echo "Input did not match with any case";
        
          }
        ?>

        I think there's something wrong with my code, when i run it in my browser, it states that:

        Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in c:\appserv\www\labs\switch.php on line 4

        I don't know what it means... 🙁 any inputs or ideas is much appreciated... 🙁

        Here is my other code, but it still doesn't work 🙁

        <html>
        <body>
        	<form name="dropdown">
        	<select name="dropdown">
        	<option value="Jehzeel1">Jehzeel1</option>
        	<option value="Jehzeel1">Jehzeel2</option>
        	<option value="Jehzeel1">Jehzeel3</option>
        	</select>
        	<input type="submit" value="submit">
        	</form>
        </body>
        </html>
        
        
        
        <?php
        switch ($_POST['dropdown'])  { 
        
        case "Jehzeel1":
         echo "Jehzeel likes apples";
        break;
        
        case "Jehzeel2":
         echo "Jehzeel likes bananas";
        break;
        
        case "Jehzeel3":
         echo "Jehzeel likes oranges";
        break;
        
        default:
          echo "Input did not match with any case";
        
          }
        ?>

          Hey, i have figured it out and it works! 🙂

          THis is my final code 🙂

          <html>
          <body>
          	<form method="post" action="?">
          	<select name="dropdown">
          	<option value="Jehzeel1">Jehzeel1</option>
          	<option value="Jehzeel2">Jehzeel2</option>
          	<option value="Jehzeel3">Jehzeel3</option>
          	</select>
          	<input type="submit" value="submit">
          	</form>
          </body>
          </html>
          
          
          
          <?php
          switch ($_POST['dropdown'])  { 
          
          case "Jehzeel1":
           echo "Jehzeel likes apples";
          break;
          
          case "Jehzeel2":
           echo "Jehzeel likes bananas";
          break;
          
          case "Jehzeel3":
           echo "Jehzeel likes oranges";
          break;
          
          default:
            echo "Input did not match with any case";
          
            }
          ?>
            12 years later

            <!DOCTYPE html>
            <html>
            <head>
            <title> switch case drop down list </title>
            </head>

            <body><center><h1>
            <form method = "post">
            	<select name = "dropdown" , value = "select">
            		<option value = "select">  select  </option>
            		<option value = "Telugu">  Telugu  </option>
            		<option value = "Hindi">   Hindi   </option>
            		<option value = "English"> English </option>
            	</select>
            	<input type = "submit", name = "submit" , value = "submit">
            </form>
            	
            		<?php
            		if ($_POST)
            		{
            			$x = $_POST['dropdown'];
            			switch($x)  
            			{ 
            
            			case "Telugu": echo "Namsthe";
            			break;
            
            			case "Hindi": echo "Namshkar";
            			break;
            
            			case "English": echo "Hello";
            			break;
            
            			default: echo "Input did not match with any case";
            
            			  }
            		}
            		?> 
            	</h1> </center>	
            </body>

            </html>

            Write a Reply...