0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
mmorcos
  • 21
  • 1
  • 1
  • 6
  • 1
    http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked – Nodir Rashidov Mar 16 '17 at 00:25
  • unchecked checkboxes do not submit any value. – Nodir Rashidov Mar 16 '17 at 00:25
  • What you're getting is the expected output, What you need to change in that ? – Sulthan Allaudeen Mar 16 '17 at 03:16
  • I would expect it to return the value of what's stored on the checkbox line no? Is it just my misunderstanding of checkboxes and html? I thought if you had *checkbox* > "foo", if i was checked it could return "foo" – mmorcos Mar 16 '17 at 03:19
  • Yeah, I started to suspect that it wasn't stored or returning correctly from the db. It posted blank as per your suggesting which will help narrow it down. Thank you! – mmorcos Mar 17 '17 at 00:02
  • Please do not add voting advice or commentary to your posts - keep them succinct and if you hang around here, and if you post good questions, you will get more upvotes than down. – halfer Apr 05 '17 at 16:49

1 Answers1

0

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 />"
mmorcos
  • 21
  • 1
  • 1
  • 6