$ _POST不会发送空复选框组

问题描述:

I have been working on a contact form for a website and I got this problem: I have 4 checkboxgroups, every each of them have at least 3 checkboxes that are available to check. We don't want them to be required to send the email. So the code is this:

$CheckboxGroup1 = array();
if(isset($_POST['submit'])){
    $name = $_POST['name'];
    $attending = $_POST['attending'];
    $CheckboxGroup1 = isset($_POST['CheckboxGroup1']) ? $_POST['CheckboxGroup1'] : 'Nothing checked';

HTML:

 <h4>What kind of set-up would you like?</h4>
 <p>Additional fees may apply for living room/specialty set-ups.</p>
 <p>
     <div class="inline-field">
         <label>
             <input type="checkbox" name="CheckboxGroup1[]" value="Living Room">
            Living Room
        </label>
        <br>
        <label>
            <input type="checkbox" name="CheckboxGroup1[]" value="Conference Room">
            Conference Room
        </label>
        <br>
        <label>
            <input type="checkbox" name="CheckboxGroup1[]" value="Other">
            Other (please specify at the end of the form)
        </label>
        <br>
    </div>
</p>

When none of the checkboxes is selected I get the message "Nothing checked" and when one of them is selected I get the value of it. The problem is when I select more than one, I get this in my email: What kind of set-up will you like?: Array (not the name of those selected).

I do not know what I have to change to make it work the right way.

Any help would be very much appreciated.

我一直致力于网站的联系表格,我遇到了这个问题: 我有4个复选框组,每个 每个人都有至少3个可供查看的复选框。 我们不希望他们发送电子邮件。 所以代码是这样的: p>

  $ CheckboxGroup1 = array(); 
if(isset($ _ POST ['submit'])){
 $ name = $ _POST [  'name']; 
 $ atte = $ _POST ['参加']; 
 $ CheckboxGroup1 = isset($ _ POST ['CheckboxGroup1'])?  $ _POST ['CheckboxGroup1']:'没有选中'; 
  code>  pre> 
 
 

HTML: p>

 &lt; h4&gt; 您想要什么样的设置?&lt; / h4&gt; 
&lt; p&gt;可能会对客厅/专业设置收取额外费用。&lt; / p&gt; 
&lt; p&gt; 
&lt; div  class =“inline-field”&gt; 
&lt; label&gt; 
&lt; input type =“checkbox”name =“CheckboxGroup1 []”value =“Living room”&gt; 
 Living room 
&lt; / label&gt  ; 
&lt; br&gt; 
&lt; label&gt; 
&lt; input type =“checkbox”name =“CheckboxGroup1 []”value =“会议室”&gt; 
会议室
&lt; / label&gt; \  n&lt; br&gt; 
&lt; label&gt; 
&lt; input type =“checkbox”name =“CheckboxGroup1 []”value =“Other”&gt; 
其他(请在表格末尾注明)
  &lt; / label&gt; 
&lt; br&gt; 
&lt; / div&gt; 
&lt; / p&gt; 
  code>  pre> 
 
 

如果没有选中任何复选框,我会得到 消息“没有检查”,当其中一个 他们被选中我得到它的价值。 问题是,当我选择多个时,我会在我的电子邮件中看到: 您想要什么样的设置?:数组 strong>(不是所选项的名称)。 p >

我不知道我需要改变什么以使其以正确的方式工作。 p>

非常感谢任何帮助。 p> DIV>

you have to change below line

Your code

$CheckboxGroup1 = isset($_POST['CheckboxGroup1']) ? $_POST['CheckboxGroup1'] :  'Nothing checked';

Change it to

if( isset($_POST['CheckboxGroup1']))
{
 $CheckboxGroup1 =implode(", ",$_POST['CheckboxGroup1']);
}
else{
  $CheckboxGroup1="Nothing checked";
}

You cannot print array with echo. Echoing array will give you string Array. Simple way in your case to get array values is to use implode:

echo implode(', ', $yourArray);