Fratyr Posted November 1, 2007 Share Posted November 1, 2007 Hello. I've just started a few weeks ago self-learning php, and there's nobody that can tell me, why my <select> and switch mode isnt working. www.fratyr.com/form.php whole code: <html> <head> <title> This is my title. </title> </head> <body> <?php function proccess_form() { global $name; global $cheese; global $times; global $onecake; global $twocake; if ($cheese == 'brie') { $cheese_message = 'It\'s brie'; } elseif ($cheese == 'cheddar') { $cheese_message = 'Its cheddar'; } else {$cheese_message = 'Its mozzarella'; } $favorite_times = count($times); if ($favorite_times <= 1) { $times_message = 'Eat more cheese.'; } elseif ($favorite_times > 1 && $favorite_times < 4) { $times_message = 'Good time to eat cheese'; } else { $times_message = 'Too much man'; } echo "Your name is $name" . "<br>"; echo "$cheese_message " . ",so you need to " . "$times_message" . "<br>"; switch ($find) { case "aa" : echo "One Cake"; break; case "bb" : echo "Two Cake"; break; default : echo "You didnt selected any cakes"; break; } } ?> <?php function display_form() { global $PHP_SELF; ?> <FORM TARGET="<?php echo $PHP_SELF; ?>" METHOD=GET> Name: <INPUT TYPE=TEXT NAME="name"><BR> What cheese do you like?<BR> <INPUT TYPE=RADIO NAME="cheese" VALUE="brie">Very Soft <INPUT TYPE=RADIO NAME="cheese" VALUE="cheddar">Farmhouse <INPUT TYPE=RADIO NAME="cheese" VALUE="mozzarella">Italian <BR> When do you eat it?<BR> <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="m">Breakfast <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="n">Lunch <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="d">Dinner <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="l">Nighty <BR> <SELECT NAME="find"> <OPTION VALUE="aa">One Piece</OPTION> <OPTION VALUE="bb">Two Pieces</OPTION> </SELECT> <INPUT TYPE=HIDDEN NAME="stage" VALUE="results"> <INPUT TYPE=SUBMIT VALUE="Thanks dude!"> </FORM> <?php } ?> <?php if (empty($stage)) { display_form(); } else { proccess_form(); } ?> </body> </html> Please :-) Quote Link to comment https://forums.phpfreaks.com/topic/75592-form-and-switch-operator-problem/ Share on other sites More sharing options...
kratsg Posted November 1, 2007 Share Posted November 1, 2007 I have a lot of things that I find horribly wrong with this code... Let me write it the way "I" would write it (although I love your tabs :-D) Do not use globals unless you REALLY need to. echo "$message <br> blah blah"; will work --> inserts the variable's value echo '$message <br> blah blah'; won't work --> literally outputs $message (the words, not the value) Let me know what you think. <?php if (!empty($_POST['stage']) || isset($_POST['stage'] && $_POST['stage'] == "results"){//does exist, has a value, and equals "results" $cheese = strip_tags($_POST['cheese']); $cheese_message = "It's ".$cheese; $favorite_times = $_POST['times'];//this is an array $i = count($favorite_times); switch($i){ case '0': case '1': $times_message = 'Eat more cheese.'; break; case '2': case '3': $times_message = 'Good time to eat cheese'; break; case '4': $times_message = 'Too much man'; break; default: $times_message = 'Eat more cheese.'; break; } echo "Your name is ".$_POST['name']."<br>"; echo "$cheese_message, so you need to $times_message<br>"; switch ($_POST['find']) { case "aa": echo "One Cake"; break; case "bb": echo "Two Cake"; break; default: echo "You didn't select any cakes."; break; } } else { ?> <html> <head> <title> This is my title. </title> </head> <body> <FORM TARGET="<?php echo $PHP_SELF; ?>" METHOD=post> Name: <INPUT TYPE=TEXT NAME="name"><BR> What cheese do you like?<BR> <INPUT TYPE=RADIO NAME="cheese" VALUE="brie">Very Soft <INPUT TYPE=RADIO NAME="cheese" VALUE="cheddar">Farmhouse <INPUT TYPE=RADIO NAME="cheese" VALUE="mozzarella">Italian <BR> When do you eat it?<BR> <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="m">Breakfast <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="n">Lunch <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="d">Dinner <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="l">Nighty <BR> <SELECT NAME="find"> <OPTION VALUE="aa">One Piece</OPTION> <OPTION VALUE="bb">Two Pieces</OPTION> </SELECT> <INPUT TYPE=HIDDEN NAME="stage" VALUE="results"> <INPUT TYPE=SUBMIT VALUE="Thanks dude!"> </FORM> </body> </html> <?php } ?> That would be my way of doing it :-) Quote Link to comment https://forums.phpfreaks.com/topic/75592-form-and-switch-operator-problem/#findComment-382496 Share on other sites More sharing options...
Fratyr Posted November 1, 2007 Author Share Posted November 1, 2007 Well the code is looks cleaner, but it wont work for me, at least. :-) 1) I never use ' ' with $vars, only with texts... 2) Can you explain the first line of code? still a bit hard to read it, although i understand it already, but maybe im wrong, the line with if(!empty... Quote Link to comment https://forums.phpfreaks.com/topic/75592-form-and-switch-operator-problem/#findComment-382501 Share on other sites More sharing options...
kratsg Posted November 1, 2007 Share Posted November 1, 2007 if (!empty($_POST['stage']) || isset($_POST['stage'] && $_POST['stage'] == "results"){//does exist, has a value, and equals "results" Break it up: if(!empty($_POST['stage'])) //if $_POST['stage'] is NOT empty if(isset($_POST['stage'])) //or if $_POST['stage'] exists if($_POST['stage'] == "results") //and if $_POST['stage'] equals "results" //then run the php, else echo the form Quote Link to comment https://forums.phpfreaks.com/topic/75592-form-and-switch-operator-problem/#findComment-382520 Share on other sites More sharing options...
Fratyr Posted November 1, 2007 Author Share Posted November 1, 2007 But, can you get this script to work? The "repaired" version of yours is not working, blank screen.... :\ Quote Link to comment https://forums.phpfreaks.com/topic/75592-form-and-switch-operator-problem/#findComment-382617 Share on other sites More sharing options...
~n[EO]n~ Posted November 1, 2007 Share Posted November 1, 2007 <?php if (!empty($_POST['stage']) && $_POST['stage'] == "results") {//does exist, has a value, and equals "results" $cheese = strip_tags($_POST['cheese']); $cheese_message = "It's ".$cheese; $favorite_times = $_POST['times'];//this is an array $i = count($favorite_times); switch($i){ case '0': case '1': $times_message = 'Eat more cheese.'; break; case '2': case '3': $times_message = 'Good time to eat cheese'; break; case '4': $times_message = 'Too much man'; break; default: $times_message = 'Eat more cheese.'; break; } echo "Your name is ".$_POST['name']."<br>"; echo "$cheese_message, so you need to $times_message<br>"; switch ($_POST['find']) { case "aa": echo "One Cake"; break; case "bb": echo "Two Cake"; break; default: echo "You didn't select any cakes."; break; } } else { ?> <html> <head> <title> This is my title. </title> </head> <body> <FORM TARGET="<?php echo $PHP_SELF; ?>" METHOD=post> Name: <INPUT TYPE=TEXT NAME="name"><BR> What cheese do you like?<BR> <INPUT TYPE=RADIO NAME="cheese" VALUE="brie">Very Soft <INPUT TYPE=RADIO NAME="cheese" VALUE="cheddar">Farmhouse <INPUT TYPE=RADIO NAME="cheese" VALUE="mozzarella">Italian <BR> When do you eat it?<BR> <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="m">Breakfast <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="n">Lunch <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="d">Dinner <INPUT TYPE=CHECKBOX NAME="times[]" VALUE="l">Nighty <BR> <SELECT NAME="find"> <OPTION VALUE="aa">One Piece</OPTION> <OPTION VALUE="bb">Two Pieces</OPTION> </SELECT> <INPUT TYPE=HIDDEN NAME="stage" VALUE="results"> <INPUT TYPE=SUBMIT VALUE="Thanks dude!"> </FORM> </body> </html> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75592-form-and-switch-operator-problem/#findComment-382620 Share on other sites More sharing options...
PHP_PhREEEk Posted November 1, 2007 Share Posted November 1, 2007 There is no need to indent all of that HTML. Try this... very modified code. Learn several ways to do something, from several different people, and you get a much wider perspective from which to choose from when you approach coding. Check the source output on any given page. There might be a few flaws, but it's pretty clean. There are MANY little coding 'easter eggs' embedded in this code... on purpose... so that you can see how to do different things depending on conditions. Try and follow this code and figure out exactly what it's doing, and why! <?php if ( $_POST['stage'] == "results" ){ $cheese = strip_tags($_POST['cheese']); $cheese_message = "It's ".$cheese; $favorite_times = $_POST['times'];//this is an array $my_message = "You seem to enjoy eating cheese"; $i = count($favorite_times); switch($i){ case '0': $my_message = 'Well, when do you eat cheese?'; break; case '1': $my_message .= ' once in awhile...'; break; case '2': $my_message .= ' an average amount of times...'; break; case '3': $my_message .= ' an above average amount of times...'; break; case '4': $my_message .= ' a LOT! Whoa! Slow down on the cheese, bro!'; break; default: $my_message = 'You manipulated the POST URL, you slick Willy!!'; break; } switch ($_POST['find']) { case "aa": $my_message2 = 'One Cake'; break; case "bb": $my_message2 = 'Two Cakes'; break; default: $my_message2 = 'you didn\'t select any cakes.'; break; } send_header(); echo "Your name is " . $_POST['name'] . " <br /> $my_message <br /> I asked you how many pieces you wanted, "; if ( $_POST['find'] == 'aa' || $_POST['find'] == 'bb' ) { echo "and you said "$my_message2"!! <br /> "; send_footer($stage = TRUE); } else { echo "but $my_message2. Too bad for you, but more for me!! <br /> "; send_footer($stage = TRUE); } } else { send_header(); echo "<form target=\"$PHP_SELF\" method=\"post\"> <p> Name: <input type=\"TEXT\" name=\"name\"></p> <p> What cheese do you like? <br /> <input type=\"radio\" name=\"cheese\" value=\"brie\">Very Soft <input type=\"radio\" name=\"cheese\" value=\"cheddar\">Farmhouse <input type=\"radio\" name=\"cheese\" value=\"mozzarella\">Italian</p> <p> When do you eat it? <br /> <input type=\"checkbox\" name=\"times[]\" value=\"m\">Breakfast <input type=\"checkbox\" name=\"times[]\" value=\"n\">Lunch <input type=\"checkbox\" name=\"times[]\" value=\"d\">Dinner <input type=\"checkbox\" name=\"times[]\" value=\"l\">Nightly</p> <br /> <select name=\"find\"> <option value=\"aa\">One Piece</option> <option value=\"bb\">Two Pieces</option> </select> <input type=\"hidden\" name=\"stage\" value=\"results\"> <input type=\"submit\" value=\"Thanks dude!\"> </form> "; send_footer(); } function send_header() { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Let's Talk Cheese!</title> </head> <body> <?php } function send_footer($stage = FALSE) { if ( $stage ) { echo "<br /> Bye Bye for now! "; } echo" </body> </html>"; } ?> PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/75592-form-and-switch-operator-problem/#findComment-382630 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.