2012-07-31 127 views
1

問:如何在我的電子郵件中顯示選定的複選框?PHP:在電子郵件中顯示覆選框數組

我創建了一個調查(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代碼(接觸.PHP):

<?php 

/* Set e-mail recipient */ 
$myEmail = "[email protected]"; 

/* 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等。不能似乎得到它的權利......

+1

有這個一個錯誤line'brand_heels = check_input($ _ POST ['brand_heels']);'在行首缺少$。 – 2012-07-31 15:16:44

回答

1
$color_flats = check_input(implode("," , $_POST['color_flats'])); 

$message = "... 

FLATS 
Pairs: $pairs_flats pairs 
Color: $color_flats 


..."; 
+0

這太棒了!它解決了我的問題!不能夠感謝你,現在我已經在這段代碼上磕磕絆絆了幾天了。再次感謝 – 2012-07-31 15:27:21

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; 

?> 
+0

我在哪裏插入此代碼?謝謝 – 2012-07-31 15:22:25

+0

@GianAguilar把變量'$ _color_flats'放在消息裏面。 – 2012-07-31 15:26:04

+0

感謝您的幫助! – 2012-07-31 15:44:57