mjtweaver Posted December 13, 2010 Share Posted December 13, 2010 Hi guys (again), thanks a lot for your previous help. I'm trying to learn about conditional statements and I've tried to create a form to produce a echo statement when all the conditions are met. Below is my code, it is in two files... baconandeggs.php <html> <head></head> <body> <form action="baconandeggsform.php" method="post"> <!-- Question One --> <p>Please choose what day it is:</p> <select name="$day"> <option value="monday">Monday</option> <option value="tuesday">Tuesday</option> <option value="wednesday">Wednesday</option> <option value="thursday">Thursday</option> <option value="friday">Friday</option> <option value="saturday">Saturday</option> <option value="sunday">Sunday</option> </select> <br /> <!-- Question Two --> <p>Please enter the time:</p> <input type="$time" name="time"> <br /> <!-- Question Three --> <p>Please choose what country you are from...</p> <select name="$country"> <option value="uk">UK</option> <option value="usa">USA</option> <option value="europe">Europe</option> </select> <!-- Form Submit --> <input type="submit"> </form> </body> </html> baconandeggsform.php <html> <head></head> <body> <?php if ($day == 'thursday') { if ($time == '0800') { if ($country == 'uk') {$meal = 'bacon and eggs';} else {$meal = 'sausages';} } } ?> <?php echo $meal; ?> </body> </html> Any ideas why it isn't working? I've probably done something really obvious, but I have been trying to figure it out for a few hours now and it's doing my head in! Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/ Share on other sites More sharing options...
Zurev Posted December 13, 2010 Share Posted December 13, 2010 Where did you learn forms? The form name should be "day" and "time", and referring to them in php is $_POST['day] and $_POST['time']. So for you you would want to do something like $day = $_POST['day']; $time = $_POST['time']; Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1146910 Share on other sites More sharing options...
mjtweaver Posted December 13, 2010 Author Share Posted December 13, 2010 Thanks for your quick reply! Sorry for being a NEWB - kinda new from coding things from scratch - I've been more a cut & paster, but trying to learn the right way. Below is the code, I have updated. It does produce the "bacon and eggs" statement when I get all the conditions right, but it doesn't produce the "sausages" statement I was looking for if you didn't produce the right conditions. baconandeggs.php <html> <head></head> <body> <form action="baconandeggsform.php" method="post"> <!-- Question One --> <p>Please choose what day it is:</p> <select name="day"> <option value="monday">Monday</option> <option value="tuesday">Tuesday</option> <option value="wednesday">Wednesday</option> <option value="thursday">Thursday</option> <option value="friday">Friday</option> <option value="saturday">Saturday</option> <option value="sunday">Sunday</option> </select> <br /> <!-- Question Two --> <p>Please enter the time:</p> <input type="time" name="time"> <br /> <!-- Question Three --> <p>Please choose what country you are from...</p> <select name="country"> <option value="uk">UK</option> <option value="usa">USA</option> <option value="europe">Europe</option> </select> <!-- Form Submit --> <input type="submit"> </form> </body> </html> baconandeggsform.php <html> <head></head> <body> <?php // Retrieve Information $day = $_POST['day']; $time = $_POST['time']; $country = $_POST['country']; // Conditional Statement if ($day == 'thursday') { if ($time == '0800') { if ($country == 'uk') {$meal = 'bacon and eggs';} else {$meal = 'sausages';} } } ?> <?php echo $meal; ?> </body> </html> Could someone point me in the right direction? Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1146919 Share on other sites More sharing options...
Maq Posted December 13, 2010 Share Posted December 13, 2010 You don't need an IF for every condition. if ($day == 'thursday' && $time == '0800' && $country == 'uk') { $meal = 'bacon and eggs'; } else { $meal = 'sausages'; } Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1146921 Share on other sites More sharing options...
mjtweaver Posted December 13, 2010 Author Share Posted December 13, 2010 Thank you very much for your help, the form is working just like I wanted. Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1146933 Share on other sites More sharing options...
mjtweaver Posted December 15, 2010 Author Share Posted December 15, 2010 I'm going through the tutorial found here: http://devzone.zend.com/node/view/id/626 I've tried adapting the code that you helped me with to include elseif statements. I can't see how my code differs from the tutorials apart from the fact I am still using && operators. The only code I have edited is on the form handler page. <html> <head></head> <body> <?php // Retrieve Information $day = $_POST['day']; $time = $_POST['time']; $country = $_POST['country']; // Conditional Statement if ($day == 'thursday' && $time == '0800' && $country == 'uk') { $meal = 'You have selected the ultimate arrangement.'; } elseif ($day == 'friday') && $time = '1000' && $country == 'usa') { $meal = 'Unlucky your American.'; } else {$meal == 'Muppet';} ?> <h2>Your selection...</h2> <?php echo $meal; ?> </body> </html> I think I have an exceptional talent for breaking pages. Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1147884 Share on other sites More sharing options...
BlueSkyIS Posted December 15, 2010 Share Posted December 15, 2010 $time = '1000' should be $time == '1000' Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1147887 Share on other sites More sharing options...
Maq Posted December 15, 2010 Share Posted December 15, 2010 You also may want to change this to: else { $meal = 'Muppet'; } Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1147888 Share on other sites More sharing options...
mjtweaver Posted December 16, 2010 Author Share Posted December 16, 2010 Again, thank you very much for your help. Working perfect now Quote Link to comment https://forums.phpfreaks.com/topic/221561-what-am-i-doing-wrong/#findComment-1148099 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.