当复选框被选中并使用POST获取数据时提交表单
Submit form when a checkbox is checked and getting data with POST
这是我的表单:
<form class="form-horizontal" method="post" action="" id="user_profile_private" name="user_profile_private">
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
我试过onChange="this.form.submit()"
和Onchange="document.getElementById('formName').submit()"
。在某些情况下,我甚至成功提交了表单,但我无法成功获取数据。我想要的是提交表单,并获得数据(如果复选框(开关)打开或关闭)在同一页面与$_POST。
PS:我用http://proto.io/freebies/onoff/做了这个开关,也许它有帮助。
重要的是通过post获取数据因为当我尝试这样做时我总是得到一个空数组var_dump($_POST);
您可以使用jquery $('selector').on('click');
或$.click()
在这个。考虑这个例子:
<?php
// PHP form handling:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$checkbox_value = $_POST['onoffswitch'];
echo '<script>alert("'.$checkbox_value.'");</script>';
}
?>
<!-- change the action="" to the name of this php file -->
<form class="form-horizontal" method="POST" action="index.php" id="user_profile_private" name="user_profile_private">
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" value="1">
<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
</form>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#myonoffswitch').on('click', function(){
if($(this).is(':checked')) {
// if your checkbox is checked, submit the form
$('#user_profile_private').submit();
}
});
});
</script>
做点类似的事情
$('.myonoffswitch').on('click',function(){
if($(".myonoffswitch").is(':checked'))
DO SOMETHING....
else
DO SOMETHING....
});
相关文章:
- 如何在php文件中获取$.post-ajax传递的值
- 如何从HTTP上下文对象中获取Post数据
- 如何使用Javascript获取POST请求填充的元素的值
- 以json形式获取post-response
- 可以'当window.location.href时,无法从javascript获取post值
- AJAX请求php获取post/get发送的指定目录中服务器上的文件列表(无JQUERY)
- PHP没有从$.ajax获取POST数据
- 获取post-jquery并在$_post中回显它(这可能吗?)
- 如何使用 AJAX 发送 POST 数据?以及如何在Web API中获取POST数据
- 如何从 Javascript AJAX 调用中获取 POST 值到 php
- 如何在jQuery中获取POST变量
- 从通过 echo 语句动态添加的表单元素中获取 POST 值
- 通过 AJAX 获取 POST 数据
- 通过 PHP 中的对象从提交按钮获取 POST
- 通过通知 URL 在 Meteor 中获取 POST 请求
- Meteor Iron路由器如何获取POST数据
- 如何通过javascript获取post()方法参数
- 在动态输入区域jquery中获取post数组以插入批代码点火器
- 如何将XMLHTTPRequest与POST和params一起正确使用?PHP未获取POST信息
- 如何在使用javascript提交表单之前获取POST内容