问题:如何在我的电子邮件中显示选定的复选框?
我已经创建了一项调查(http://www.hello-rio.com/surveyonshoes/),并且在我提交表单时,我试图让我选中的复选框显示在我的电子邮件中。对于这个我来说,我是一个完整的菜鸟,任何帮助将不胜感激
继承我的html示例复选框代码(在index.html中):
<label for="colors">What colors do you own? (check all that apply)</label>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="black">black </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="brown">brown </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="beige">beige </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="white">white </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="gold">gold </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="silver">silver </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="red">red </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="blue">blue </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="yellow">yellow </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="green">green </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="orange">orange </div>
<div class="twocol"><input type="checkbox" name="color_flats[]" value="purple">purple </div><br>
<label for="others">Others</label>
<input type="text" name="color_flats[]" class="others" />
以及完整的php代码(contact.php):
<?php
/* Set e-mail recipient */
$myEmail = "johndoe@domain.com";
/* Check all form inputs using check_input function */
$subject = "Survey on Shoes";
$Name = check_input($_POST['Name'], "Enter your name");
$Address = check_input($_POST['Address']);
$Email = check_input($_POST['Email']);
$Age = check_input($_POST['Age']);
$Sex = check_input($_POST['Sex']);
$Status = check_input($_POST['Status']);
$Employment = check_input($_POST['Employment']);
$Income = check_input($_POST['Income']);
$pairs_flats = check_input($_POST['pairs_flats']);
$color_flats = check_input($_POST['color_flats']);
$size_flats = check_input($_POST['size_flats']);
$material_flats = check_input($_POST['material_flats']);
$brand_flats = check_input($_POST['brand_flats']);
$frequency_flats = check_input($_POST['frequency_flats']);
$cost_flats = check_input($_POST['cost_heels']);
$pairs_heels = check_input($_POST['pairs_heels']);
$color_heels = check_input($_POST['color_heels']);
$size_heels = check_input($_POST['size_heels']);
$material_heels = check_input($_POST['material_heels']);
brand_heels = check_input($_POST['brand_heels']);
$frequency_heels = check_input($_POST['frequency_heels']);
$cost_heels = check_input($_POST['cost_heels']);
$height_heels = check_input($_POST['height_heels']);
$work = check_input($_POST['work']);
$mall = check_input($_POST['mall']);
$events = check_input($_POST['events']);
$travel = check_input($_POST['travel']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $Email))
{
show_error("E-mail address not valid");
}
/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
$website = '';
}
/* Let's prepare the message for the e-mail */
$message = "Hello!
Your form has been submitted by:
Name: $Name
Address: $Address
Email: $Email
Age: $Age
Sex: $Sex
Status: $Status
Employment: $Employment
Income: $Income
FLATS
Pairs: $pairs_flats pairs
Color: $check_msg
Size: $size_flats
Material: $material_flats
Brand: $brand_flats
Frequency: $frequency_flats pairs a year
Cost: Php $cost_flats
HEELS
Pairs: $pairs_heels pairs
Color: $color_heels
Size: $size_heels
Material: $material_heels
Brand: $brand_heels
Frequency: $frequency_heels pairs a year
Cost: $cost_heels
Height: $height_heels inches
Work/School: $work
Mall: $mall
Events: $events
Travel: $travel
End of message
";
/* Send the message using mail() function */
mail($myEmail, $subject, $message);
/* 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>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>
我已经尝试了各种各样的代码,如内幕,if语句,foreach等等。似乎无法正确...
答案 0 :(得分:1)
$color_flats = check_input(implode("," , $_POST['color_flats']));
$message = "...
FLATS
Pairs: $pairs_flats pairs
Color: $color_flats
...";
答案 1 :(得分:1)
由于$color_flats
是一个数组,因此需要在每个
foreach($color_flats as $whatever){
echo $whatever;
}
或者你可以将其内爆以制作一个唯一的字符串:
echo implode(',', $color_flats);
<?php
# Default Vars
$_color_flats = '';
if(isset($color_flats) === TRUE){
# Is Array ?
if(is_array($color_flats) === TRUE){
# Count
$c = count($color_flats);
# Loop
for($i=0; $i < $c; $i++){
$_color_flats.= (isset($color_flats[$i]) === TRUE ? $color_flats[$i] : '').($i == ($c-1) ? '' : ($i == $c-2 ? ' and ' : ', '));
}
}
}
echo $_color_flats;
?>