Greetings. I have a small issue that I can't seem to wrap my head around.
I have a form on my .php page. Within that form, I dynamically generate a radio button by using a while loop. Basically it goes off and checks my SUBCAT table and pulls all the categories (db field: catcode) and associated subcategories (db field: subcat_code). I then display these in one Radio button (as
catcode: subcat_code), with the intention being that by selecting catcode: subcat_code, I can then pass both values to the database.
Current Code
$display_block .= "<input type=\"radio\" name=\"subcat_code\" value=\"$subcode\" /><strong>$catcode:</strong> $subcode<br />
<input type=\"hidden\" name=\"catcode\" value=\"$catcode\">";
It displays on screen like this with the radio button:
Category: Subcategory
Fruit: Citrus
Fruit: Melons
Vegetables: Legumes
Vegetables: Roots
When I SUBMIT, however, the subcat_code is passed correctly, but the hidden catcode always defaults to the last category in the list ... so in the above example, if I click on the
Fruit: Citrus button, my database would be populated with category: Vegetables, subcategory: Citrus.
I assume that this is because it is picking up on the last "inputted" category - which it thinks is the last category retrieved, not the one associated with the selected radio button.
Anyone know if it's possible to do what I'm trying to do, or what I'm missing here? I know I could do the cat, subcat in separate radio buttons, but I was trying to avoid having the user create their own cat/subcat combinations - hence the attempt to display only the existing combinations, and pass both variables based on the selected subcategory.
Thanks!
Portion of Code from Form (Printing Radio Button)
// start loop to display available cat/subcat
while ($set = mysql_fetch_array($setCat_res)) {
$subcode = $set[code];
$catcode = $set[catcode];
// portion to get and display categories ends below at the <input portion
$display_block .= "<input type=\"radio\" name=\"subcat_code\" value=\"$subcode\" /><strong>$catcode:</strong> $subcode<br />
<input type=\"hidden\" name=\"catcode\" value=\"$catcode\">";
} //ends while loop to retrieve categories
Portion of Code (Inserting the Record)
//update table
$sqlIns = "INSERT INTO correct_table VALUES('$_POST[id]','$_POST[subcat_code]','$_POST[catcode]')";
mysql_query($sqlIns,$conn) or die(mysql_error());