【问题标题】:PHP checkboxes won't send Yes/No values in emailPHP 复选框不会在电子邮件中发送是/否值
【发布时间】:2014-06-22 02:27:03
【问题描述】:

我正在尝试通过制作一个简单的披萨订购系统来熟悉 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();
}
?>

【问题讨论】:

  • 旁注:$_POST['Half Onions'] 等中的空格应不惜一切代价避免。使用下划线作为分隔符。
  • 显示您的 HTML 表单。就目前而言,您的问题属于“不清楚”类别,然后是一些

标签: php html email post checkbox


【解决方案1】:

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

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

而不是

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

【讨论】:

    【解决方案2】:

    你没有设置你的变量,你只是将它们呼应出来:

    /* 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...
    

    因为价值并不重要。

    【讨论】:

    • 旁注:pepperoniPepperoni 完全是两个不同的动物。除非 OP 两者都有。更多的原因是我没有给出答案。我们不知道 OP 表单中的内容。
    • 哦,如果不尝试,永远不知道 ;-)
    • @Fred-ii- 但一般的答案是,主要问题是回显而不是分配。我认为...
    • 当然,包括单词之间的空格。
    • 现在终于有阳光了,我正在考虑自己“推迟”这个工作日,干杯! ;-)
    【解决方案3】:

    我认为变量中有空格,例如“Half Pepperoni”或“Half Mushrooms”!!

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 2012-01-19
      • 2014-05-30
      相关资源
      最近更新 更多