我无法在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.
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 />"