2013-05-14 121 views
1

我想讓我的php表單顯示哪些收音機已被選中。換句話說,當有人通過我的網站通過電子郵件與我聯繫時,我希望它顯示他們打勾的電臺。我嘗試了一切,但無法讓它工作!PHP複選框電子郵件不起作用

HTML代碼迄今 -

<table width="308" border="0"> 
     <label> 
     <input type="radio" name="servicetype[]" value="checkbox" id="Technical Support" /> 
     Technical Support</label> 

     <label> 
     <input type="radio" name="servicetype[]" value="checkbox" id="Information Services" /> 
     Information Services</label> 
    <tr> 
    <td align="left"> 
    <input name="Submit" type="submit" id="Submit" class="contact-class2" value="Send"/> 
    </td> 
    </tr> 
</table> 
    </form> 

PHP到目前爲止

<?php 
$field_name = $_POST['cf_name']; 
$field_email = $_POST['cf_email']; 
$field_message = $_POST['cf_message']; 

$mail_to = 'www.example.com'; 
$subject = 'hello'; 

$servicetype = join(", ", $_REQUEST["servicetype"]); 
if (isset($_POST['servicetype'])) { 
    foreach ($_POST['servicetype'] as $key => $val) { 
     // do what you need 
    } 
} 


$body_message = ''.$field_message."\n"."\n"; 
$body_message .= 'From: '.$field_name; 

$headers = 'From: '.$field_email."\r\n"; 
$headers .= 'Reply-To: '.$field_email."\r\n"; 

$mail_status = mail($mail_to, $subject, $body_message, $headers ); 



if ($mail_status) { ?> 
    <script language="javascript" type="text/javascript"> 
     window.location = 'Contact form.html'; 
    </script> 
<?php 
} 
else { ?> 
    <script language="javascript" type="text/javascript"> 
     window.location = 'Contact form.html'; 
    </script> 
<?php 
} 
?> 
+0

$ _ POST將得到您的單選按鈕的值屬性,而不是ID。另外,如果他們是單選按鈕,你需要發送一個數組嗎?因爲你只能挑選一個。 – andrewsi 2013-05-14 17:26:22

+0

當我發送表單時,它只顯示字段名稱和標題等,但不顯示任何複選框。整個領域是完全空白的。我是PHP的超級新手,所以我絕望地需要幫助:( – user2382274 2013-05-14 17:57:04

回答

2

您誤內置的複選框上輸入。 value屬性應該是顯示的複選框的「名稱」,例如,

<input type="checkbox" name="servicetype[]" value="Technical Support" id="Technical Support" /> 
       ^^^^^^^^        ^^^^^^^^^^^^^^^^^ 

這是value與表格一起提交。就目前而言,您只需提交「複選框」一詞,並且無法知道選擇了哪個複選框。

您的郵箱,簡直是:

$body_message = "blah blah blah. Requested services: " . implode(',' $_POST['servicetype']); 
+0

「implode」是什麼意思?你認爲什麼是最好的方式來顯示在電子郵件中選擇了哪個複選框?我不認爲PHP編碼我用來顯示哪個複選框被選中是正確的 – user2382274 2013-05-14 17:58:20

+0

'join()'是'implode()'的別名。 – 2013-05-14 18:49:00