0
我有4個複選框,其中之一是Others
有文本框,我想獲得用戶檢查的所有值,如果他檢查其他選項從文本框中獲取值與Others
複選框關聯。如何從文本框中的值在複選框在php
HTML代碼
<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代碼
$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;
}
}
所以如果用戶檢查'others',你是否只需要文本框的值或者兩者都檢查和文本值 –
備註:ID's應該是唯一的,並且你所有的複選框都帶有'id =「Pro_chkbox」'和你標記爲「javascript」,沒有支持的代碼;這是爲什麼? –
我需要檢查和文本值,例如:如果用戶檢查香蕉和其他人,並在文本框中輸入蘋果,所以結果應該是:「香蕉,蘋果」 – Bnabil