Jump to content

What am I doing wrong?


mjtweaver

Recommended Posts

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!  :o

 

 

 

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. :D

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.