
Untitled
By: a guest on
Apr 27th, 2012 | syntax:
None | size: 1.87 KB | hits: 16 | expires: Never
Switch statement in PHP
<?php
switch($_POST["city"]){
case "sf":
echo "Welcome to San Francisco, enjoy the beautiful weather.";
break;
case "tokyo":
echo "Welcome to Tokyo, enjoy the sushi.";
break;
case "paris";
echo "Welcome to Paris, enjoy the Eiffel Tower.";
break;
Default:
echo "Please pick a city.";
echo "<a href="/week7.html">Go Back</a>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form action="Week7PHP.php" method="post">
<input type="radio" name="city" value="sf">San Francisco<br />
<input type="radio" name="city" value="paris">Paris<br/>
<input type="radio" name="city" value="tokyo">Tokyo<br/>
<input type="submit" id="Submit">
</form>
</div>
</body>
</html>
<?php
if(isset($_POST['city'])){
$city=$_POST['city'];
}else{
$city='';
}
//Or simpler
$city=(isset($_POST['city']))?$_POST['city']:'';
//The use $city
switch($city){
case "sf":
echo "Welcome to San Francisco, enjoy the beautiful weather.";
break;
case "tokyo":
echo "Welcome to Tokyo, enjoy the sushi.";
break;
...
...
?>
<?php
switch($_POST["city"]){
case "sf":
echo "Welcome to San Francisco, enjoy the beautiful weather.";
break;
case "tokyo":
echo "Welcome to Tokyo, enjoy the sushi.";
break;
case "paris":
echo "Welcome to Paris, enjoy the Eiffel Tower.";
break;
default:
echo "Please pick a city.";
echo "<a href="/week7.html">Go Back</a>";
break;
}
?>
if (isset($_POST["city")) {
/* switch statement */
} else {
/* form element*/
}