php Ajax 发布并获取复选框值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21164884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax post and get checkbox value
提问by Andreas
I have this code:
我有这个代码:
<script type="text/javascript">
function processForm() {
$.ajax( {
type: 'POST',
url: '/ajax/checkbox.php?name=foton',
data: { checked_box : $('input:checkbox:checked').val()},
success: function(data) {
$('#message').html(data);
}
} );
}
</script>
<input type="checkbox" name="foton" value="1" onclick="processForm()">
checkbox.php file:
checkbox.php 文件:
$checkbox = intval($_POST['foton']);
if($checkbox == 1){
mysql_query("UPDATE users SET sekretessFoton = 1 WHERE userID = $memberID");
}else{
mysql_query("UPDATE users SET sekretessFoton = 0 WHERE userID = $memberID");
}
The problem is that i dont get any value from $_POST['foton'] What is wrong?
问题是我没有从 $_POST['foton'] 中得到任何价值 有什么问题?
Thanks
谢谢
采纳答案by AleVale94
Edit your code in:
编辑您的代码:
$.ajax( {
type: 'POST',
url: '/ajax/checkbox.php',
data: { foton : $('input:checkbox:checked').val()},
success: function(data) {
$('#message').html(data);
}
} );
/ajax/checkbox.php?name=foton
Here you have a $_GETparameter that's called nameand its valueis foton.
在这里,你有一个$_GET参数,这就是所谓的name和值是foton。
In my code you're sending the value of your checkbox in a $_POSTparametercalled foton.
在我的代码你发送你的复选框中的值$_POST参数叫foton。
You don't have to set a query string in your $.ajaxurl when you specify data (instead of you wantto redirect to a page which will process your $_GETparameters a part).
$.ajax当您指定数据时,您不必在url 中设置查询字符串(而不是您想要重定向到将处理您的$_GET参数的页面)。
回答by Nouphal.M
You are trying to access a POST varible that isn't defined or sent.
您正在尝试访问未定义或未发送的 POST 变量。
Try
尝试
$checkbox = intval($_POST['checked_box']);
In you ajax you are sending data as checked_box. Also you are not posting any thing else this checked box value. The only other value that you are sending is through query string ?name=fotonwhich could be accessed via $_GET['name']. I hope you get the point.
在您的 ajax 中,您将数据作为checked_box. 此外,您不会发布此复选框值的任何其他内容。您发送的唯一其他值是通过查询字符串?name=foton,可以通过$_GET['name']. 我希望你明白这一点。
回答by Awlad Liton
You have post ajax with this:
你用这个发布了ajax:
data: { checked_box : $('input:checkbox:checked').val()},
here checked_box is the name of your parameter. you need to get value by this name.
这里checked_box 是你的参数名称。您需要通过此名称获取价值。
Try this:
尝试这个:
$checkbox = intval($_REQUEST['checked_box']);

