上周末在做一个提交表单到服务器进行处理的页面时,遇到了一个问题,困扰了我整整一个下午.
就是当我提交表单到服务器端时,$_POST数组有其它的表单域值,包括<input type='text'...>, <textarea>, <input type='submit'...>. 可是就是没有<input type='checkbox'...>. 我有两个checkbox, 代码如下:
<form name='form1' method='post' action='check.php'>
<input type='checkbox' name='checkbox' checked='checked' disabled='disabled' value='checkbox1' />Box1<br />
<input type='checkbox' name='checkbox' value='checkbox2' />Box2<br />
Name: <input type='text' name='text' /><br />
Introducation: <textarea name='intro' cols='20' rows='5'></textarea><br />
<input type='submit' name='submit' value='提交' />
</form>
我百度了一个这个问题, 发现大家都在说PHP对表单中的复选框有特殊要求, 名字要写成数组样式的, 就像这样:
<input type='checkbox' name='checkbox[]' value='checkbox2' />
然后在服务端从$_POST中取得一个数组,进行遍历就可以得到想要的值了.
我按照这个方法做了,可是还是无法在$_POST中找到复选框的值.
然后我又尝试将两个checkbox分别命名成不同的名字,
<input type='checkbox' name='checkbox1' checked='checked' disabled='disabled' value='checkbox1' />Box1<br />
<input type='checkbox' name='checkbox2' value='checkbox2' />Box2<br />
可是还是不行.
最后,只好把网上的例子原封不动地复制下来,在本地调试,发现可以传过去. 然后就一点点儿修改代码, 终于找到了原因. 原来是因为我加了这一句: disabled='disabled' , 导致所有的checkbox都无法传到服务端.
我只所以加这一句,是想设置一项默认选中,并且用户无法修改. 现在看来是不行了. 把disabled去掉就行了.