我有4个复选框,其中一个是Others
有文本框,我想获取用户选中的所有值,如果他检查了其他值,则选项以获取与{{1}相关联的文本框中的值复选框。
HTML代码
Others
PHP代码
<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>
答案 0 :(得分:1)
这将为您解决问题
$checked_count = count($_POST['check_list']);
$productlist = ''; //initialize an empty string for product list
if ($checked_count > 1) //check if multiple check-boxes are checked
{
$productlist = implode(', ', $_POST['check_list']); //implode all checkbox values in list string
if(in_array('Others', $_POST['check_list'])) { //check if others is checked
$productlist .= ', '.$_POST['otherproduct']; //con-cat text in text box lined with others in list string
$productlist = str_replace('Others,', '', $productlist); //remove others from the list string (skip if you want others to be in your result'
}
} elseif ($checked_count == 1) {
$productlist = ($_POST['check_list'][0] == 'Others') ? $_POST['otherproduct'] : $_POST['check_list'][0]; //if only one checkbox is checked then check its value and use the value
}
echo "<br/>".$productlist;
此外,您可以将客户端和服务器端验证确保从表单中获得正确的输入值。
此外,您可以使用java脚本使您的表单更具互动性。