0

I'm trying to familiarize myself with PHP by making a simple pizza ordering system that emails size, toppings, and the orderer's information. The email sends nicely, but the toppings section of the email is blank. What am I missing?

Thanks!

<?php
/* Set e-mail recipient */
$myemail = "[email protected]";

$subject = "Pizza Order";





/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$email = check_input($_POST['email'], "Enter your email");
$street = check_input($_POST['street'], "Enter your your street");
$apt = check_input($_POST['apt'], "Enter your your apartment number");
$zip = check_input($_POST['zip'], "Enter your ZIP code");
$phone = check_input($_POST['phone'], "Enter your phone number");
$comments = $_POST['comments'];


/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}


/* the following code is currently not working */
$pepperoni = $_POST['pepperoni'];


if(isset($_POST['pepperoni']) && 
   $_POST['Pepperoni'] == 'Yes') 
{
    echo "pepperoni";
}
else
{
    echo "";
}    


if(isset($_POST['Half Pepperoni']) && 
   $_POST['halfpepperoni'] == 'Yes') 
{
    echo "halfpepperoni";
}
else
{
    echo "";
}

if(isset($_POST['Onions']) && 
   $_POST['onions'] == 'Yes') 
{
    echo "onions";
}
else
{
    echo "";
} 

if(isset($_POST['Half Onions']) && 
   $_POST['halfonions'] == 'Yes') 
{
    echo "halfonions";
}
else
{
    echo "";
}

if(isset($_POST['Mushrooms']) && 
   $_POST['mushrooms'] == 'Yes') 
{
    echo "mushrooms";
}
else
{
    echo "";
} 

if(isset($_POST['Half Mushrooms']) && 
   $_POST['halfmushrooms'] == 'Yes') 
{
    echo "halfmushrooms";
}
else
{
    echo "";
}  

if(isset($_POST['Peppers']) && 
   $_POST['peppers'] == 'Yes') 
{
    echo "peppers";
}
else
{
    echo "";
}

if(isset($_POST['Half Peppers']) && 
   $_POST['halfpeppers'] == 'Yes') 
{
    echo "halfpeppers";
}
else
{
    echo "";
} 

if(isset($_POST['Extra Cheese']) && 
   $_POST['extracheese'] == 'Yes') 
{
    echo "extracheese";
}
else
{
    echo "";
}

if(isset($_POST['Half Extra Cheese']) && 
   $_POST['halfextracheese'] == 'Yes') 
{
    echo "halfextracheese";
}
else
{
    echo "";
}

if(isset($_POST['Sausage']) && 
   $_POST['sausage'] == 'Yes') 
{
    echo "sausage";
}
else
{
    echo "";
} 

if(isset($_POST['Half Sausage']) && 
   $_POST['halfsausage'] == 'Yes') 
{
    echo "halfsausage";
}
else
{
    echo "";
}                






/* Let's prepare the message for the e-mail */
/* -=-=-=- EDITED -=-=-=- The toppings should be uncommented BUT you need to make variables like above Likewise the checkboxes need to have associated. 

here's annother example variable: 
$pepperoni = $_POST['pepperoni'];

*/


$message = "


Toppings:
$pepperoni
$halfpepperoni
$onions
$halfonions
$mushrooms
$halfmushrooms
$peppers
$halfpeppers
$extracheese
$halfextracheese
$sausage
$halfsausage


Name: $name
Email: $email
Street: $street
Apt: $apt
ZIP: $zip
Phone: $phone
Comments: $comments

";




$headers = "From:" . $email;





/* Send the message using mail() function */
/*mail($name, $email, $apt, $zip, $phone, $comments $pepperoni $halfpepperoni $onions $halfonions $mushrooms $halfmushrooms $peppers $halfpeppers $extracheese $halfextracheese $sausage $halfsausage);*/
mail($myemail,$subject,$message,$headers);


/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>
2
  • Sidenote: Spaces in $_POST['Half Onions'] etc. should be avoided at all cost. Use underscores as a seperator. Commented May 5, 2014 at 17:38
  • 1
    Show your HTML form. As it stands, your question falls under the "unclear" category, and then some. Commented May 5, 2014 at 17:54

3 Answers 3

1

Replace all space between the textfield name, Ex: use halfonions instead of using textname with space like Half Onions

if(isset($_POST['halfonions']) &&   $_POST['halfonions'] == 'Yes') {

instead of

if(isset($_POST['Half Onions']) &&   $_POST['halfonions'] == 'Yes') {
Sign up to request clarification or add additional context in comments.

Comments

1

You are not setting your variables, you are only echoing them out:

/* the following code is currently not working */
$pepperoni = $_POST['pepperoni'];


if(isset($_POST['pepperoni']) && 
   $_POST['Pepperoni'] == 'Yes') 
{
    echo "pepperoni";
}
else
{
    echo "";
}

Now the $pepperoni variable will contain Yes if it was selected and nothing else. And that is the only variable you are currently trying to set, the rest of the variables in your message is undefined.

You probably want something like:

if(isset($_POST['pepperoni']) && 
   $_POST['pepperoni'] == 'Yes') 
{
    $pepperoni = "pepperoni";
}
else
{
    $pepperoni = "";
}

And that for all the variables you use in your message.

And you can reduce that to:

$pepperoni = isset($_POST['pepperoni']) ? 'pepperoni' : '';
                           ^ or however it is spelled in the html...

as the value does not really matter.

9 Comments

Sidenote: pepperoni and Pepperoni are two seperate animals altogether. Unless OP has both. All the more reasons I didn't put in an answer. We don't know what's inside the OP's form.
Oh well, one never knows if one does not try ;-)
@Fred-ii- But the general answer stands, echoing instead of assigning is the main problem. I think...
Definitely, including the spaces between words.
With the sunshine finally being here right now, I'm thinking of "postponing" this work day myself, cheers! ;-)
|
0

I think there are spaces in the variables like "Half Pepperoni" or "Half Mushrooms" !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.