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 />"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?