Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something fishy radio VS checkbox

Status
Not open for further replies.

webdev007

Programmer
Sep 9, 2005
168
Inserting in Db results of multiple radio

<input type='checkbox' name='aa[]' value='1'> WORKS

<input type='radio' name='aa[]' value='1'> Does not
AND
<input type='radio' name='aa[$counter]' value='1'> Does not.

The two radio result in "Undefined offset:0" at line ... whis is a !preg_match ...[$counter]
but if I do print_r the array is correct
AND
if I comment the whole preg section
then at the "DB insert level" I got undefined var $answer
I really do not get it!
 
Code:
<input type='radio' name='aa[$counter]' value='1'> Does not.
[/quote]

variables do not get expanded within single quotes. could this be the issue? alternatively if this is outside a php block, you have to use a construct like this
Code:
<input type='radio' name='aa[[red]<? echo $counter;?>[/red]]' value='1'> Does not.

also, remember that only actually checked checkboxes will be submitted by the browser. so when you are explicitly setting the array key in the html you are not guaranteed to have a zero based array in the output.

so instead of a for loop across the results you would need to use a foreach

Code:
//NOT
for($i=0; $i<count($_POST['aa']); $i++){
 echo "value of checkbox $i is " . $_POST['aa'][$i];
 echo "<br/>";
}
//USE INSTEAD
foreach ($_POST['aa'] as $key=>$val){
 echo "value of checkbox $key is $val";
 echo "<br/>";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top