user3605262
user3605262

Reputation: 1

PHP checkboxes won't send Yes/No values in email

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();
}
?>

Upvotes: 0

Views: 143

Answers (3)

jeroen
jeroen

Reputation: 91734

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.

Upvotes: 1

3mmaar
3mmaar

Reputation: 83

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

Upvotes: 0

Krish R
Krish R

Reputation: 22711

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') {

Upvotes: 1

Related Questions