doudao1837 2013-06-25 18:04
浏览 58
已采纳

添加复选框到PHP POST电子邮件表单

I'm trying to build a form for wordpress. I've used plugins in the past but I need maximum control for some specific styling. I'm not very good with PHP yet, so am struggling trying to add checkboxes to the script.. I've removed my attempts and left the checkboxes in the html, but not in the PHP - can someone advise me of the best way to make any selected checkboxes visible in the email that is sent? Everything else works at the moment.

the 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="general@mycompany.com">General</option> <option value="support@mycompany.com">Support</option> <option value="sales@mycompany.com">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> 

the 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:

"; foreach($fields as $a => $b){   $body .= sprintf("%20s: %s
",$b,$_REQUEST[$a]); } 

 $headers2 = "From: noreply@YourCompany.com"; 
 $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 webmaster@YourCompany.com"; } 
 }
}
 ?> 

Thanks! MC

  • 写回答

1条回答 默认 最新

  • dongtan6206 2013-06-25 18:15
    关注

    An array of checkboxes would be most appropriate. By using [] in the checkbox names, PHP will automatically parse them into a native array.

       <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> 
    

    On the PHP side:

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

    Outputs (if all checked)

    Selected Projects: Web, Digital, Consultancy

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c