将复选框状态传递给PHP [英] Passing checkbox state to PHP

查看:98
本文介绍了将复选框状态传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < input type =hiddenname =check_box_1value =0/> 
< input type =checkboxname =check_box_1value =1/>

这个工作正常,但是当你点击submit时,勾选复选框时,它会同时传递隐藏的值和原来的复选框值在PHP中的$ _POST变量,这可以避免吗?

我有隐藏的值,所以未勾选的复选框被传递给$ _POST变量以及勾号。

解决方案

更好的方法是删除隐藏字段,只需检查在PHP中:

  if($ _POST ['check_box_1'] =='1'){/ * /} 
else {/ *做一些未挑战的事情* /}


<input type="hidden" name="check_box_1" value="0" />
<input type="checkbox" name="check_box_1" value="1" />

This works fine, however when you click on submit, and the checkbox is ticked, it passes BOTH the hidden value and the original checkbox value to the $_POST variable in php, can this be avoided?

I have the hidden value there, so that unticked checkboxes are passed to the $_POST variable as well as the ticked ones.

解决方案

The better approach is to remove the hidden field, and simply have a check in PHP:

if ($_POST['check_box_1']=='1') { /*Do something for ticked*/ }
else { /*Do something for unticked*/ }

这篇关于将复选框状态传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆