doushu5805
2017-09-16 11:01
浏览 146
已采纳

如何从php中的复选框中的文本框中获取值

I have 4 checkboxes, one of them is Others have textbox, i want to get all the values that the user checked and if he checked others option to get the values from the textbox associated with Others checkbox.

HTML Code

<div class="row">
    <div class="col-sm-4">
        <label class="Modallabel">Available Products:</label>
    </div>
    <div class="col-sm-8">
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" value="Cacao">Cacao</label>
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" value="Coconuts">Coconuts</label>
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" value="Bananas">Bananas</label><br>
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" id="optcheck" value="Others">Others</label>
        <input type="text" id="Other_pro" name="otherproduct"><br>
        <label id="Note">(Separate Products with commas)</label>
    </div>
</div>

PHP Code

$checked_count = count($_POST['check_list']);

    if ($checked_count > 1)
    { 
        $productlist = implode(', ', $_POST['check_list']);
        echo $productlist;
    }
    elseif ($checked_count == 1)
    { 
        foreach($_POST['check_list'] as $selected) {
            $productlist = $selected;

            //To check if Others checkbox is checked or not to get the values in textbox
            if ($productlist == "Others")
            {
                $productlist = $_POST["otherproduct"];
            }
            echo $productlist;
        }
    }

图片转代码服务由CSDN问答提供 功能建议

我有4个复选框,其中一个是 Others 有文本框,我想得到 用户选中的所有值以及是否检查了其他值选项以从与 Others 关联的文本框中获取值。

HTML代码

 &lt; div class =“row”&gt; 
&lt; div class =“col-sm-4”&gt; 
&lt; label class =“  Modallabel“&gt;可用产品:&lt; / label&gt; 
&lt; / div&gt; 
&lt; div class =”col-sm-8“&gt; 
&lt; label id =”Pro_chkbox“class =”复选框 -  inline“&gt;&lt; input name =”check_list []“type =”checkbox“value =”Cacao“&gt; Cacao&lt; / label&gt; 
&lt; label id =”Pro_chkbox“class =”checkbox-inline“&gt;  &lt; input name =“check_list []”type =“checkbox”value =“Coconuts”&gt; Coconuts&lt; / label&gt; 
&lt; label id =“Pro_chkbox”class =“checkbox-inline”&gt;&lt;输入名称 =“check_list []”type =“checkbox”value =“Bananas”&gt; Bananas&lt; / label&gt;&lt; br&gt; 
&lt; label id  =“Pro_chkbox”class =“checkbox-inline”&gt;&lt; input name =“check_list []”type =“checkbox”id =“optcheck”value =“其他”&gt;其他&lt; / label&gt; 
&lt;输入 type =“text”id =“Other_pro”name =“otherproduct”&gt;&lt; br&gt; 
&lt; label id =“Note”&gt;(带逗号的单独产品)&lt; / label&gt; 
&lt; / div&gt  ; 
&lt; / div&gt; 
   
 
 

PHP代码

  $ checked_count =  count($ _ POST ['check_list']); 
 
 if($ checked_count&gt;  1)
 {
 $ productlist = implode(',',$ _POST ['check_list']); 
 echo $ productlist; 
} 
 elseif($ checked_count == 1)
 {
 foreach  ($ _POST ['check_list']为$ selected){
 $ productlist = $ selected; 
 
 //检查是否选中了Others复选框以获取textbox中的值
 if($ productlist ==  “其他”)
 {
 $ productlist = $ _POST [“otherproduct”]; 
} 
 echo $ productlist; 
} 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题