2012-10-16 65 views
1

已經用PHP構建了一個功能表單,但是我有一個包含5個選項的複選框(可以選擇多於1個),但是隻有1個選項出現在電子郵件中。PHP表單上的多個複選框選項

HTML:

<label class="radio"><input type="checkbox" name="service" value="Web Design" />Web Design</label> 
<label class="radio"><input type="checkbox" name="service" value="Graphic Design" />Graphic Design</label> 
<label class="radio"><input type="checkbox" name="service" value="Brand Identity" />Brand Identity</label> 
<label class="radio"><input type="checkbox" name="service" value="Online Marketing" />Online Marketing</label> 
<label class="radio"><input type="checkbox" name="service" value="Other" />Other</label> 

PHP:($服務)

<?php 
$name = $_POST['name']; 
$email = $_POST['email']; 
$phone = $_POST['phone']; 
$website = $_POST['website']; 
$service = $_POST['service']; 
$description = $_POST['description']; 
$inspiration = $_POST['inspiration']; 
$budget = $_POST['budget']; 
$deadline = $_POST['deadline']; 
$formcontent=" From: $name \n Email: $email \n Phone: $phone \n Website: $website \n\n service: $service \n budget: $budget \n deadline: $deadline \n\n description: $description \n\n inspiration: $inspiration "; 
$recipient = "[email protected]"; 
$subject = "Contact form"; 
$mailheader = "From: $email \r\n"; 
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); 
echo "Thank You, your information has been received."; 
?> 

回答

1

如果你有多個同名的元素,那麼你只能讀取其中的一個,除非使用數組語法[] eg name=service[]然後您將收到$ _POST ['service']作爲包含所有服務的數組。您可以使用implode將其轉換爲字符串。

$service = implode(' ', $_POST['service']); 
+0

這工作完美。謝謝大家的快速響應! –

0

name="service[]"(陣列),而不是在你的形式獲得多個選擇的值作爲數組name="service"。雖然檢索,你可以得到使用implode函數值作爲像這樣的複選框如下

$services = implode(",", $_POST['service']); 
0

用戶數組名稱,而不是簡單的名字:

<label class="radio"><input type="checkbox" name="service[]" value="Web Design" />Web Design</label> 
<label class="radio"><input type="checkbox" name="service[]" value="Graphic Design" />Graphic Design</label> 
<label class="radio"><input type="checkbox" name="service[]" value="Brand Identity" />Brand Identity</label> 
<label class="radio"><input type="checkbox" name="service[]" value="Online Marketing" />Online Marketing</label> 
<label class="radio"><input type="checkbox" name="service[]" value="Other" />Other</label> 

print_r($_POST['service']); 
// output 
Array 
(
    [0] => Web Design 
    [1] => Graphic Design 
    [2] => Brand Identity 
    [3] => Online Marketing 
    [4] => Other 
) 
1

在名稱中使用數組,在那裏命名它service service there,service []。 然後使用多個= 「多」 tag.and在PHP部分使用:

<?php 
$services=implode(',',mysql_real_escape_string($_post['service'])); 
print_r($services); 
?> 

編碼快樂!