Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Can 1 radio btn (in while loop) pass values to 2 separate db fields?
Old 03-24-2008, 11:53 AM Can 1 radio btn (in while loop) pass values to 2 separate db fields?
Junior Talker

Posts: 1
Name: Alison
Trades: 0
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());
caila is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-25-2008, 11:00 AM Re: Can 1 radio btn (in while loop) pass values to 2 separate db fields?
reli4nt's Avatar
Extreme Talker

Posts: 168
Location: New York
Trades: 0
If you look at the html of the page generated you'll see that you have several hidden fields with the same name.

The hidden inputs are independent of the radio fields so when you submit the form the server gets 4 different values for catcode and will naturally be left with only the last one. There ia no "association" with a radio field.

I recommend that you put the values together in the radio button:

PHP Code:
// 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_cat_code\" value=\"$subcode"_" "$catcode\" /><strong>$catcode:</strong> $subcode<br />";

//ends while loop to retrieve categories 
And then split (explode()) the value before you insert it into the db:

PHP Code:
$bothCodesArray explode($_POST['subcat_cat_code'] );
$subcat_code $bothCodesArray[0];
$catcode $bothCodesArray[1];

//validate values first

$sqlIns "INSERT INTO correct_table VALUES('$_POST[id]','$subcat_code','$catcode')";
mysql_query($sqlIns,$conn) or die(mysql_error()); 
NOTE: you need to do some validation here before you insert the posted values into your database or else you are open to sql injection attacks. If the cat code are integers then simple casting may work (e.g. (int)$subcat_code) but otherwise try mysql_real_escape_string($subcat_code). Never assume the values submitted from a form are what you intended.

EDIT: Use quotes for strings as in $set['code'] versus $set[code].
__________________

Please login or register to view this content. Registration is FREE

Designing the world we live in.
Defining the terms we live by.

Last edited by reli4nt; 03-25-2008 at 11:04 AM..
reli4nt is offline
Reply With Quote
View Public Profile Visit reli4nt's homepage!
 
Reply     « Reply to Can 1 radio btn (in while loop) pass values to 2 separate db fields?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.04754 seconds with 12 queries