<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()
解决方案 »
- 怎么我装的phpstduy打不开?
- 明天就要PHP面试了
- 关于php的简单的cookie问题求助
- 求教PHP正则匹配问题
- 提交给当前网页form的action属性是什么啊?
- phpmyadmin中添加字段时可以设定位于哪个字段之前或之后,原理是什么,这怎么实现啊
- 本人,学习、使用php有三年多时间,在其期间得到csdn各位高人的指点,前后,共做出了5,6十个采用php+mysql的网站,同时,兼有内部小型mis
- [急][在线]两个简单的问题,请大家帮忙看看
- 急呀~~! 高手請進,Excel 報表出錯.``````````````````````在線等.
- 关于正则表达式的问题!
- 请教一个生成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是数据库的编号),在服务器端进行循环即可!
这种方法感觉扩展性不太好!
如果前端投票的数量是不定的,只能来一个上限值来控制了,感觉不太爽