3 回答

TA貢獻1785條經驗 獲得超4個贊
未提交的單選或復選框元素未提交,因為它們不被視為成功。因此,您必須檢查是否使用isset或empty函數發送了它們。
if (isset($_POST['checkbox'])) {
// checkbox has been checked
}

TA貢獻1946條經驗 獲得超4個贊
我不時使用這種技術:
<input type="hidden" name="the_checkbox" value="0" />
<input type="checkbox" name="the_checkbox" value="1" />
注意:在不同的服務器端語言中,這會有不同的解釋,因此如有必要,請進行測試和調整。感謝SimonSimCity的提示。

TA貢獻1805條經驗 獲得超9個贊
這是一個使用javascript的簡單解決方法:
在提交包含復選框的表單之前,請將“關閉”的復選框設置為0并選中它們以確保它們已提交。例如,這適用于復選框數組。
/////示例//////
給定一個id =“ formId”的表單
<form id="formId" onSubmit="return formSubmit('formId');" method="POST" action="yourAction.php">
<!-- your checkboxes here . for example: -->
<input type="checkbox" name="cb[]" value="1" >R
<input type="checkbox" name="cb[]" value="1" >G
<input type="checkbox" name="cb[]" value="1" >B
</form>
<?php
if($_POST['cb'][$i] == 0) {
// empty
} elseif ($_POST['cb'][$i] == 1) {
// checked
} else {
// ????
}
?>
<script>
function formSubmit(formId){
var theForm = document.getElementById(formId); // get the form
var cb = theForm.getElementsByTagName('input'); // get the inputs
for(var i=0;i<cb.length;i++){
if(cb[i].type=='checkbox' && !cb[i].checked) // if this is an unchecked checkbox
{
cb[i].value = 0; // set the value to "off"
cb[i].checked = true; // make sure it submits
}
}
return true;
}
</script>
- 3 回答
- 0 關注
- 745 瀏覽
相關問題推薦
添加回答
舉報