dongshou2024 2017-03-16 00:04
浏览 46
已采纳

我无法在PHP中获取我的数组复选框值

I can't find out why this isn't working so please don't shred me for asking this question although it's been answered. I have a HTML array of checkboxes and I'd like to get the value of what is checked. I'm getting the questions via a cURL request.

I've tried several combinations of changing variable names, adding index values, using the

foreach ($var1 as $var1 => $value)

and still nothing.

PHP to display the questions:

      $exquest = curl_exec($ch); 
      curl_close($ch);
      $exquest = json_decode($exquest, true); 

      $numOfQuests = $exquest["questcount"]; 

      for ($i=0; $i<$numOfQuests; $i++) 
      {
      echo "<input type='checkbox' name='q[]' value='" . $exquest['question$i'] . "'> " . $exquest["question$i"] . "<br /><br />";
      }

PHP to collect checked questions:

if(is_array($_POST['q'])){
    foreach($_POST['q'] as $value){
  print_r($value);
  var_dump($value);
      }
   }

OUTPUT:

The outputs I've gotten thus far are either "Array" repeated the number of times equal to the number of boxes checked, or [0]=> 0, [1] => [1], etc but even then it only goes by total number checked not the sequence (i.e index 0, 2, and 4) it'll always come back 0,1,2 etc.

  • 写回答

1条回答 默认 最新

  • dongyanggan3025 2017-03-23 22:12
    关注

    As @Ryan Vincent suggested, by checking var_dump($_POST); I determined that nothing was being sent from one page to the other aside form the N number of checked boxes."

    Solution: I was missing the $i in q[] for name and id. Thanks all!

         echo "<input type='checkbox' name='q[$i]' id='q[$i]' value='" . $exquest["question$i"] . "'> " . $exquest["question$i"] . "<br /><br />"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器