<form action="Untitled-2.php">
Question1:
<label><input id="vote_1" value="1-1" type="radio" />Answer1</label>
<label><input id="vote_1" value="1-2" type="radio" />Answer2</label>
<label><input id="vote_1" value="1-3" type="radio" />Answer3</label>
<br>
Question2:
<label><input id="vote_5" value="5-1" type="radio" />Answer1</label>
<label><input id="vote_5" value="5-2" type="radio" />Answer2</label>
<label><input id="vote_5" value="5-3" type="radio" />Answer3</label>
<input name="submit" type="submit" />
</form>
Untitled-2.php
//我想把前台选中的radio进行一下循环
//例如Question1的Answer1被选中,Question2的Answer2被选中,那在服务器端就循环2次
//总之就是把前台选中的radio放到数组中,在服务器端做处理
for()
解决方案 »
- 变量后面的->之后的内容无法编译
- 重装mysql,最有一步"Applay security"报错,求解!
- 初学PHP,想向各位请教在WIN XP下如何配置php运行环境,另外我想使用sql server做为数据库,请问环境怎么搭建?谢谢
- PHP 结果集问题
- 为什么在验证验证码的时候程序不能正确验证,请各位高手相助
- 请问php的设置是否正确?
- 怎么样去掉文件类型的后缀
- 谁能提供用 php 阅读和发送 E-Mail 完整代码。(2)
- 一个很菜的问题:PHP的安全性
- 为什么一个php页面只有一个字母,但是访问的时候却会被加上<!DOCTYPE>
- 请教一个生成word问题
- 如何在批处理中打开php网页,并等待返回
Question1:
<label><input name="vote_1" value="1-1" type="radio" />Answer1</label>
<label><input name="vote_1" value="1-2" type="radio" />Answer2</label>
<label><input name="vote_1" value="1-3" type="radio" />Answer3</label>
<br>
Question2:
<label><input name="vote_2" value="5-1" type="radio" />Answer1</label>
<label><input name="vote_2" value="5-2" type="radio" />Answer2</label>
<label><input name="vote_2" value="5-3" type="radio" />Answer3</label>
<input name="submit" type="submit" />
</form>$_POST["vote_$i"]就可以了
那如果再加一个Question3呢?
radio的name是vote+voteid组成,而投票的ID并不一定是连续的。
$_POST["vote_$i"]这样是不是不太可取啊
Question3:
<label><input name="vote_5" value="5-1" type="radio" />Answer1</label>
<label><input name="vote_5" value="5-2" type="radio" />Answer2</label>
<label><input name="vote_5" value="5-3" type="radio" />Answer3</label>
<form action="Untitled-2.php">
Question1:
<label><input id="vote_1" value="1-1" type="radio" />Answer1</label>
<label><input id="vote_1" value="1-2" type="radio" />Answer2</label>
<label><input id="vote_1" value="1-3" type="radio" />Answer3</label>
<br>
Question2:
<label><input id="vote_5" value="5-1" type="radio" />Answer1</label>
<label><input id="vote_5" value="5-2" type="radio" />Answer2</label>
<label><input id="vote_5" value="5-3" type="radio" />Answer3</label>
<input type="button" value="button" onclick="checkRadio();"/>
</form> <script>
function checkRadio()
{
var input = document.getElementsByTagName('input');
for(var i=0,n=input.length;i<n;i++)
{
if(input[i].getAttribute('type') == 'radio' && input[i].checked)
{
alert(input[i].getAttribute('id') + ' => ' + input[i].getAttribute('value'));
}
}
}
</script>
如果form提交,也可以用上面的结果,赋值给隐藏一个input,服务端接收
当然,常见的接收方式上面已有答复
foreach($r[0] as $k)
$radio[] = $_POST[$k];
print_r($radio);按你的假设,将得到 Array ( [0] => 1-1 [1] => 5-2 )
<form action="Untitled-2.php">
Question1:
<label><input id="vote_1[]" name="vote_1[]" value="1-1" type="radio" />Answer1</label>
<label><input id="vote_1[]" name="vote_1[]" value="1-2" type="radio" />Answer2</label>
<label><input id="vote_1[]" name="vote_1[]" value="1-3" type="radio" />Answer3</label>
<br>
Question2:
<label><input id="vote_5[]" name="vote_5[]" value="5-1" type="radio" />Answer1</label>
<label><input id="vote_5[]" name="vote_5[]" value="5-2" type="radio" />Answer2</label>
<label><input id="vote_5[]" name="vote_5[]" value="5-3" type="radio" />Answer3</label>
<input name="submit" type="submit" />
</form>
$array1 = $_POST["vote_1"];
$array5 = $_POST["vote_5"];
for...........
<input name="vote_1" ...
<input name="vote_1" ...
Q2<input name="vote_2" ...
<input name="vote_2" ...Q3
<input name="vote_3" ...
<input name="vote_3" ...这样才能实现批量处理,如果你要的name随便写,那只能采用
$array1 = $_POST["vote_1"];
$array5 = $_POST["vote_5"];
之类的写法
用连续的,for循环就出来了
$radiogroup = '1-2,3-4';
$radiogroupArray = explode ( ',', $radiogroup );
foreach ($radiogroupArray as $Onevote){
}这个怎么总提示错误?
基础太差...
Question1:
<label><input name="vote[1]" value="1-1" type="radio" />Answer1</label>
<label><input name="vote[1]" value="1-2" type="radio" />Answer2</label>
<label><input name="vote[1]" value="1-3" type="radio" />Answer3</label>
<br>
Question2:
<label><input name="vote[2]" value="2-1" type="radio" />Answer1</label>
<label><input name="vote[2]" value="2-2" type="radio" />Answer2</label>
<label><input name="vote[2]" value="2-3" type="radio" />Answer3</label>
<br>Question3:
<label><input name="vote[5]" value="5-1" type="radio" />Answer1</label>
<label><input name="vote[5]" value="5-2" type="radio" />Answer2</label>
<label><input name="vote[5]" value="5-3" type="radio" />Answer3</label> <input name="submit" type="submit" />
</form>$_POST["vote"]//数组
1:
楼上的一些朋友已经说过了
做一个hidden,在表单提交前把选择的radio记录下来后,用逗号分割每个选项,而后到服务器端进行explode and for循环即可。
2:
在前端动态生成radio时做一个name的顺序编号(每一个radio的value是数据库的编号),在服务器端进行循环即可!
这种方法感觉扩展性不太好!
如果前端投票的数量是不定的,只能来一个上限值来控制了,感觉不太爽