vanhien13 Posted May 2, 2009 Share Posted May 2, 2009 I was wondering if someone can show me how to use switch case instead of if for the code below. Here is the original task Create a form that asks a user to select a destination from a list of radio buttons. The choices are: San Francisco, New York, London, Paris, Honolulu, and Tokyo. Write a PHP script that uses a switch statement to evaluate each of the cities, and based on the city selected by the user, sends back a message such as, "Welcome to San Francisco. Be sure to bring your heart and a jacket!" or "Bonjour, let's take a stroll on the left bank!". The code below works, but i don't know how to write it using switch case. Thanks, Hien <html> <head> <?php if (isset($_POST['method'])) { $selected_city = $_POST['info']; print $selected_city; } ?> <title>Where are you heading to?</title> </head> <body> <br> <form name="form1" method="post" action="ques9.php"> <p> <label> <input type="radio" name="info" value="Expect breezy weather!" > San Francisco</label> <br> <label> <input type="radio" name="info" value="Remember to go see the Statue of Liberty!"> New York</label> <br> <label> <input type="radio" name="info" value="If you get a chance, say hi to the Queen Mother!"> London</label> <br> <label> <input type="radio" name="info" value="Bonjour, please see the Effiel Towel!"> Paris</label> <br> <label> <input type="radio" name="info" value="Let's hit the beach!"> Honolulu</label> <br> <label<input type="radio" name="info" value="How about some sake and sushi later?"> Tokyo</label> <br> </p> <input type="submit" name="method" value="Destination"/> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/156589-radio-button-with-switch-case/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 You shouldn't ask people to write stuff for you. Read rule #7 - http://www.phpfreaks.com/page/forum-rules But a switch works like this: switch ($_POST['method']) { case 'London': echo 'something'; break; // more cases here default: echo ''; } Link to comment https://forums.phpfreaks.com/topic/156589-radio-button-with-switch-case/#findComment-824493 Share on other sites More sharing options...
Philip Posted May 2, 2009 Share Posted May 2, 2009 Smells like homework to me Link to comment https://forums.phpfreaks.com/topic/156589-radio-button-with-switch-case/#findComment-824533 Share on other sites More sharing options...
vanhien13 Posted May 3, 2009 Author Share Posted May 3, 2009 Why don't you go fuck yourself your nolife computer geek, I did not you to write my code for me. I just need a hint how the alternative works. Didn't you asshole every need help when first learning programming. I don't need a lecture from a moron who split his time between jerking off and his computer. Don't be an ass Link to comment https://forums.phpfreaks.com/topic/156589-radio-button-with-switch-case/#findComment-824644 Share on other sites More sharing options...
Philip Posted May 3, 2009 Share Posted May 3, 2009 I was wondering if someone can show me how to use switch case instead of if for the code below. Here is the original task Create a form that asks a user to select a destination from a list of radio buttons. The choices are: San Francisco, New York, London, Paris, Honolulu, and Tokyo. Write a PHP script that uses a switch statement to evaluate each of the cities, and based on the city selected by the user, sends back a message such as, "Welcome to San Francisco. Be sure to bring your heart and a jacket!" or "Bonjour, let's take a stroll on the left bank!". Calm down there buddy You saying "can you show me how to..." and then listing the original task as if you copy/pasted it from a assignment looks mighty suspicious if you ask me. And yes I needed help when I first started, but I learned to read the manual and google'd examples then tried it myself. Also, I find it highly ironic that you said: Don't be an ass Link to comment https://forums.phpfreaks.com/topic/156589-radio-button-with-switch-case/#findComment-824646 Share on other sites More sharing options...
sKunKbad Posted May 3, 2009 Share Posted May 3, 2009 switch($selected_city){ case 'london': echo 'remember to drink some tea'; break; case 'los angeles': echo 'remember to pack your firearm for protection'; break; case 'tokyo': echo 'remember your soy sauce'; break; } Link to comment https://forums.phpfreaks.com/topic/156589-radio-button-with-switch-case/#findComment-824651 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.