<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()

解决方案 »

  1.   

    <form action="Untitled-2.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"]就可以了 
      

  2.   


    那如果再加一个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>
      

  3.   

    有没有别的方法来实现,例如说hidden
      

  4.   

    遍历一下即可
      <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>
      

  5.   

    楼上的兄弟,能在PHP端做处理吗
      

  6.   

    php端接收,,你如果AJAX请求,则直接在符合条件的里面拼接字符串,发送到服务端即可
    如果form提交,也可以用上面的结果,赋值给隐藏一个input,服务端接收
    当然,常见的接收方式上面已有答复
      

  7.   

    preg_match_all('/\bvote_\d+/', join(',', array_keys($_POST)), $r);
    foreach($r[0] as $k) 
      $radio[] = $_POST[$k];
    print_r($radio);按你的假设,将得到 Array ( [0] => 1-1 [1] => 5-2 ) 
      

  8.   


     <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...........
      

  9.   

    1,作为批量处理,你的name应当是连续的,设置id对服务器端无用Q1
    <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"];
    之类的写法
      

  10.   

    radio的name你可以不用数据库的id,设置成连续的,你的value已经有id了,比如5-2那你的5不就是id吗,为什么还要在name上再设置一次呢?
    用连续的,for循环就出来了
      

  11.   


     $radiogroup = '1-2,3-4';
       $radiogroupArray = explode ( ',', $radiogroup );
       
             foreach ($radiogroupArray as $Onevote){
            
             }这个怎么总提示错误?
    基础太差...
      

  12.   

    <form action="Untitled-2.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="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"]//数组
      

  13.   

    2种方法:
    1:
    楼上的一些朋友已经说过了
    做一个hidden,在表单提交前把选择的radio记录下来后,用逗号分割每个选项,而后到服务器端进行explode and for循环即可。
    2:
    在前端动态生成radio时做一个name的顺序编号(每一个radio的value是数据库的编号),在服务器端进行循环即可!
    这种方法感觉扩展性不太好!
    如果前端投票的数量是不定的,只能来一个上限值来控制了,感觉不太爽