2013-06-25 50 views
1

我正在嘗試爲wordpress構建表單。過去我使用過插件,但我需要最大限度地控制某些特定的樣式。我對PHP並不擅長,所以我一直在努力向腳本中添加複選框。我已經刪除了我的嘗試,並將複選框留在了html中,但不在PHP中 - 有人能告訴我最好的方法在發送的電子郵件中顯示任何選中的複選框?目前一切正常。將複選框添加到PHP POST電子郵件表格

的HTML:

<form method="post" action="<?php bloginfo('template_directory'); ?>/contact.php"> 

<h3>Your Details</h3> 

<div class="formrow"> 
    <input type="text" name="Name" maxlength="99" id="fullname" placeholder="Name" /> 
    <input type="email" name="Email" maxlength="99"placeholder="Email Address" /> 
    <input type="tel" name="Phone" maxlength="25" placeholder="Phone Number" /> 
</div> 

<h3>Project Type</h3> 

<div class="formrow"> 
    <fieldset> 
     <input type="checkbox" name="project1" value="Web"> 
     <label for="type1">Web</label> 
     <input type="checkbox" name="project2" value="Digital"> 
     <label for="type2">Digital</label> 
     <input type="checkbox" name="project3" value="Consultancy"> 
     <label for="type3">Consultancy</label> 
    </fieldset> 
</div> 

<table align=center> 
<tr><td colspan=2><strong>Contact us using this form:</strong></td></tr> 
<tr><td>Department:</td><td><select name="sendto"> <option value="[email protected]">General</option> <option value="[email protected]">Support</option> <option value="[email protected]">Sales</option> </select></td></tr> 


<tr><td>Company:</td><td><input size=25 name="Company"></td></tr> 

<tr><td>Subscribe to<br> mailing list:</td><td><input type="radio" name="list" value="No"> No Thanks<br> <input type="radio" name="list" value="Yes" checked> Yes, keep me informed<br></td></tr> 
<tr><td colspan=2>Message:</td></tr> 
<tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr> 
<tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr> 
<tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr> 
</table> 

</form> 

的PHP:

<?php 
$to = $_REQUEST['sendto'] ; 
$from = $_REQUEST['Email'] ; 
$name = $_REQUEST['Name'] ; 
$headers = "From: $from"; 
$subject = "Web Contact Data"; 

$fields = array(); 
$fields{"Name"} = "Name"; 
$fields{"Company"} = "Company"; 
$fields{"Email"} = "Email"; 
$fields{"Phone"} = "Phone"; 
$fields{"list"} = "Mailing List"; 
$fields{"Message"} = "Message"; 

$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

$headers2 = "From: [email protected]"; 
$subject2 = "Thank you for contacting us"; 
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com"; 

if($from == '') {print "You have not entered an email, please go back and try again";} 
else { 
if($name == '') {print "You have not entered a name, please go back and try again";} 
else { 
$send = mail($to, $subject, $body, $headers); 
$send2 = mail($from, $subject2, $autoreply, $headers2); 
if($send) 
{print "Success";} 
else 
{print "We encountered an error sending your mail, please notify [email protected]"; } 
} 
} 
?> 

謝謝! MC

+0

複選框'name'應像'projects []'然後''_POST ['projects'];' – Ronnie

回答

8

一組複選框將是最合適的。通過在複選框名稱中使用[],PHP將自動將它們解析爲本機數組。

<input type="checkbox" name="projects[]" value="Web"> 
    <label for="type1">Web</label> 
    <input type="checkbox" name="projects[]" value="Digital"> 
    <label for="type2">Digital</label> 
    <input type="checkbox" name="projects[]" value="Consultancy"> 
    <label for="type3">Consultancy</label> 

在PHP端:

$selectedProjects = 'None'; 
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){ 
    $selectedProjects = implode(', ', $_POST['projects']); 
} 

$body .= 'Selected Projects: ' . $selectedProjects; 

輸出(如果所有選中)

選擇項目:網絡,數碼,諮詢

+0

謝謝,這是完美的,我有幾個PHP的概念只是在我的大腦中點擊 - 非常感謝。 – Mike

+0

感謝您的出色解決方案!完美的作品! –

相關問題