Jump to content

Confused on 'switch' statement


simcoweb

Recommended Posts

I'm delving into 'switch' statements instead of if/else to test the waters. I'm confused on a couple of things. First, i've created a simple form assuming I can populate the $var with a post from a form field then have it check through the 'switch' statement, find the right case and display the appropriate code. Simple!

 

here's my form:

 

<?php
if (isset($_POST['submit'])){
// let us test out a switch parameter
$guess = "$_POST['guess']";

switch ($guess)
{
  case 1:
  echo "You chose first one";
  break;
  case 2:
  echo "You chose second one";
  break;
  case 3:
  echo "You chose third one";
  break;
  default:
  echo "Make a choice you idiot!";
}
}
?>
<html>
<head>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text" size="20" name="guess">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

 

I'm assuming the form field 'guess' would populate the $guess variable which would then run through the case statements for a match? What I am missing here? This doesn't work. :(

Link to comment
Share on other sites

<?php
if (isset($_POST['submit'])){
// let us test out a switch parameter
switch ($_POST['guess'])
{
  case 1:
  echo "You chose first one";
  break;
  case 2:
  echo "You chose second one";
  break;
  case 3:
  echo "You chose third one";
  break;
  default:
  echo "Make a choice you idiot!";
}
}
?>
<html>
<head>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text" size="20" name="guess">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

Link to comment
Share on other sites

Toon, I edited my original post right afterward realizing that I had to set the $guess = $_POST['guess']; but somehow left the quotes there by accident. I had $guess = "3"; before that and in my haste I took out the 3 but left the quotes.

 

Anyway, I can't get it to work using the $_POST. Here's my code again and taking into consideration the advice to move it further down as well.

 

<html>
<head>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text" size="20" name="guess">
<input type="submit" value="submit" name="submit">
</form>
<?php
if (isset($_POST['submit'])){
// let us test out a switch parameter
$guess = $_POST['guess'];

switch ($guess)
{
  case 1:
  echo "You chose first one";
  break;
  case 2:
  echo "You chose second one";
  break;
  case 3:
  echo "You chose third one";
  break;
  default:
  echo "Make a choice you idiot!";
}
}
?>
</body>
</html>

 

Basically nothing happens. The form just submits and nothing is displayed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.