simcoweb Posted June 8, 2007 Share Posted June 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 8, 2007 Share Posted June 8, 2007 $guess = "3"; change to $guess =$_POST['gues']; You may also widsh to put the switch further down - inside the body tag otherwise you may not notice it upthere in teh top corner. Quote Link to comment Share on other sites More sharing options...
Dragen Posted June 8, 2007 Share Posted June 8, 2007 <?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> Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 8, 2007 Author Share Posted June 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
brissy_matty Posted June 8, 2007 Share Posted June 8, 2007 <?php switch($_POST['guess']) { case "1": //Your code here.... break; } ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 9, 2007 Share Posted June 9, 2007 By default, a form's method is "GET", add method="post" to the form tag: <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> Ken Quote Link to comment Share on other sites More sharing options...
simcoweb Posted June 9, 2007 Author Share Posted June 9, 2007 Hehehehe.... dohhhh...forgot to put method="POST" in the form Ok, got it to work now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.