PHP复选框不会在电子邮件中发送是/否值

时间:2014-05-05 17:36:48

标签: php html email post checkbox

我正在尝试通过制作一个简单的披萨订购系统来熟悉PHP,该系统通过电子邮件发送大小,配料和订购者的信息。电子邮件很好地发送,但电子邮件的顶部部分是空白的。我错过了什么?

谢谢!

<?php
/* Set e-mail recipient */
$myemail = "katrina.skovan@gmail.com";

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

3 个答案:

答案 0 :(得分:1)

替换文本域名称之间的所有空格,例如:使用halfonions而不是使用带有Half Onions

空格的textname
if(isset($_POST['halfonions']) &&   $_POST['halfonions'] == 'Yes') {

而不是

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

答案 1 :(得分:1)

你没有设置你的变量,你只是回应它们:

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


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

现在$pepperoni变量将包含Yes(如果已选中),而不包含任何内容。这是您当前尝试设置的唯一变量,消息中的其余变量未定义。

您可能需要以下内容:

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

对于您在邮件中使用的所有变量。

你可以将其减少到:

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

因为价值并不重要。

答案 2 :(得分:0)

我认为变量中有空格,如“半意大利辣味香肠”或“半蘑菇”!!