如何从html表单获取未经检查的复选框到php并为其赋值?(how can I get an unchecked checkbox from a html form to php and give a value to it?)
我正在博客中工作,我正在尝试处理注册表单的新闻通讯复选框...当用户标记复选框时我没有问题得到'是'作为结果我把它带到数据库,但当用户离开复选框未选中时,我得到一个未定义的索引,只是无法继续使用寄存器,这是我到目前为止的代码
html表单
该
//declared variable for the users input
$newsletter=$_POST['newsletter'];
//the condition for the checkbox
if(isset( $newsletter)){
$newsletter=="yes";
}else{
$newsletter=="no";
}
//then I insert the variable $newsletter through a prepared query...
I'm working in a blog and I'm trying to deal with the newsletter checkbox of the register form... when the user marks the checkbox I have no problem to get 'yes' as result and I take that to the data base, but when the user leaves the checkbox unchecked I got an undefined index and just can't continue with the register, here's the code I have so far
the html form
the
//declared variable for the users input
$newsletter=$_POST['newsletter'];
//the condition for the checkbox
if(isset( $newsletter)){
$newsletter=="yes";
}else{
$newsletter=="no";
}
//then I insert the variable $newsletter through a prepared query...
原文:https://stackoverflow.com/questions/36680658
更新时间:2020-02-13 06:03
最满意答案
使用空检查。
if(!empty($_POST['newsletter'])){
}
Use empty check.
if(!empty($_POST['newsletter'])){
}
相关问答
我们可以使用JavaScript来做到这一点,不需要jQuery。 只需传递更改后的元素并让JavaScript处理它。 HTML
syn
JS function doalert(checkboxElem) {
if (checkboxElem.checked) {
...
如果取消选中,复选框不会返回任何值? 所以,如果你想要检查它是否被检查,你最好使用PHP的isset方法。 例如: $isFree = isset($_POST['free'] 然后使用这个$isFree变量插入到DB中。 Checkbox doesn't return any value if it is unchecked? So, if you want such to check whether it was checked or not you better use the isset
...
这里是代码看看它是否工作 - <?php
session_start();//session start should be in the beginning of the code
if(isset($_POST) && !empty($_POST)){
$la = isset($_POST['la']) ? 1 : 0; // if the box is checked, return 1. if not, return 0
if($la == 1 ||
...
使用它来获得正确的值。 $("#terms_and_conditions").is(':checked')
Use this instead to get the right value. $("#terms_and_conditions").is(':checked')
嗨试试这个代码, $('#Submit1').click(function () {
var grid = $("#grid12").data("kendoGrid");
var selected = grid.tbody.find('td').find('.chkbxq').is(':checked');
var checkid = grid.tbody.find('td').find('.chkbxq').attr('
...
好的,我所知道的是你要反转复选框。 如果已经检查过,请不要使用表单发送值。 如果未选中,则使用表单发送值。 如果您想在复选框点击时提交表单,我只是没有得到。 使用具有该值的相同名称进行隐藏输入。 当复选框被选中时,禁用隐藏盒。
$('#my_
...
两件小事: function checkAll() {
var checks = document.getElementsByName("checkGroup[]");
for (var i=0; i < checks.length; i++) {
checks[i].checked = true;
}
}
getElementByName应该是getElementsByName ,而checkGroup应该是checkGroup[] 。 除此之外你的代码
...