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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List box updates hidden field in form 3

Status
Not open for further replies.

CTekMedia

Programmer
Oct 5, 2001
634
US
I have a list box that is dynamically populated with a table that contains 2 fields - NAME - COLUMN. When a user selects a NAME from the listbox I need the corresponding COLUMN value to be entered into a hidden field.

Any suggestions? Any books out there that deal with all of the tricky things that can be done with form fields?

Any books out there that could turn me into a real programmer? Any way to get the description of myself as a programmer on this forum removed????

Arrrggghhh

Thanks in advance everyone.

Cheers,

Bluetone
 
Here is the outline. You can do a onChange() on the list field. in the onchange, you will do a

document.forms[0].hiddenField.value=
document.forms[0].listField.options[listField.selectedIndex].value


I am assuming you are doing something like
<select name="listField" onChange="updateHiddenField()">
<cfloop query="getrecords">
<cfoutput>
<option value="#getrecords.column#">#getrecords.name#</option>
</cfoutput>
</cfloop>
</select>

regards,
Brian
 
could you please show a few lines of the html for this list box?

if it's like this --

<select name="foo">
<option value="1">name 1</option>
<option value="2">name 2</option>
<option value="3">name 3</option>
</select>

then i have to ask, why isn't this good enough, why the need for a hidden field?

r937.com | rudy.ca
 
Thanks folks.

Rudy,

In this case the DB is badly designed but I need to know how to do this in general anyway.

Think a pull down box that automatically updates the contents of a text field in the form - except I am using a hidden field rather than the second box.

I have looked at some JS but am hoping their might be a way to do this using CF - otherwise I will have to incorporate the dynamic aspect of this data into the JS and I'm weaker at that than CF. I'm thinking along the lines of pull the Name and Category data pair - convert it to an array and then when the user selects a Name the second item in the array - the Category - is entered into the hidden form field.

Cheers all!

Cheers,

Bluetone
 
yes, but why a hidden field?

this doesn't appear to be a database design issue at all

now, form fields don't do anything unless the form is submitted, and i guess the part i don't understand is why wouldn't the selected name be submitted in its list box field, why does it have to be a hidden field in the first place

in bluetone's example, the value will be submitted in the form field called listField



r937.com | rudy.ca
 
It is a hidden field because I don't want the user to be able to change it. Once they select a NAME I want the corresponding COLUMN value put in the hidden field. Then when the form is submitted this NAME and COLUMN value will be inserted into a third table. There is a bit of a database issue because if it was designed properly there would not be redundant data in the DB - there wouldn't be this third table to deal with.

I'm hacking through Brian's post now. Thanks both!

Cheers,

Bluetone
 
Once they select a NAME I want the corresponding COLUMN value put in the hidden field"

and if they should change the name chosen before they submit?

i still don't see how this is a database issue

don't you have an opportunity to use coldfusion logic on the form fields?

r937.com | rudy.ca
 
If they change the NAME before they submit then the value in the hidden field (COLUMN) will have to change with it.

Basically what I need is just a listbox that will modify the contents of another field - but dynamically populated. Brian's post has me headed down the right road although I'm not sure where to put:

document.forms[0].hiddenField.value=
document.forms[0].listField.options[listField.selectedIndex].value

Body tag?

Cheers,

Bluetone
 
I don't think this is a CF question. If you want a hidden field to change whenever a user makes a selection, that's going to be client side and you'll need JavaScript, it has nothing to do with CF.

If you want the user to make the selection, submit the form, THEN assign a value, that's server side and you'll use CF. But if you wait until they submit the form, you still don't need a hidden field, you can just let CF figure out what to do with some cfif's or a query or something based on what the form selection is.


Hope This Helps!

ECAR
ECAR Technologies, LLC

"My work is a game, a very serious game." - M.C. Escher
 
I see where the apples / oranges confusion exists. I was hoping for a CF solution (per my post above) but will use Brian's JS if necessary. Looks like JS might be easier in this case anyway.

Cheers,

Bluetone
 
CF solution? piece of cake

<CFPARAM NAME="form.listfield" DEFAULT="">
<CFSET variables.anotherfield = form.listfield>

then use variables.anotherfield wherever you were going to use the hidden field

r937.com | rudy.ca
 
Ok here is what I ended up with - I used CF.

The original INSERT looked like this (simplified):

<CFIF IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "form1">

<CFQUERY datasource="CTest">
INSERT INTO tbl_main (UName, FullName, ItemName, SPath, SCategory)
VALUES (
'#FORM.UName#',
'#FORM.FullName#',
'#FORM.ItemName#',
'#FORM.SPath#',
'#FORM.SCategory#'
)
</CFQUERY></CFIF>


I changed it to this - added SColumn to the insert then looked up the value using a query. I just burned 8 hours on something stupidly simple.

<CFIF IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "form1">

<CFQUERY name="Recordset_Col" datasource="CTest">
SELECT SColumn
FROM LT_category
WHERE SCategory = '#FORM.SCategory#' AND UName = '#SESSION.mm_username#'
</CFQUERY>

<CFQUERY datasource="CTest">
INSERT INTO tbl_main (UName, FullName, ItemName, SPath, SCategory, SColumn)
VALUES (
'#FORM.UName#',
'#FORM.FullName#',
'#FORM.ItemName#',
'#FORM.SPath#',
'#FORM.SCategory#',
'#Recordset_Col.SColumn#'
)
</CFQUERY>
</CFIF>

Thanks for the help everyone as usual.

Cheers,

Bluetone
 
Glad you got it working.

I think where rudy is confused, and so am I, is why the hidden field to be equal to the drop down?

lets say you DID get the JS to work
Code:
<select name = "mySelect" [b]onChange = "document.forms[0].hiddenField.value = this.value"[/b]> <!--- the easier way to do it. --->
<option value="1">name 1</option>
<option value="2">name 2</option>
<option value="3">name 3</option>
</select>
<input type = "hidden" name = "hiddenField">

when you submit the form you'll have 2 variables with the same value.

form.mySelect and form.hiddenField There isn't any point in that.

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
You could also put two values in the drop down value.

<option value="name 1|col 2">name 1</option>
<option value="name 2|col 5">name 2</option>
<option value="name 3|col 5">name 3</option>

then when you do your insert
Code:
VALUES (
'#FORM.UName#',
'#FORM.FullName#',
'#FORM.ItemName#',
'#FORM.SPath#',
'#listFirst(FORM.SCategory, "|")#',
'#listLast(FORM.SCategory, "|")#'
)

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
I must have mistated something above. Sorry for the confusion guys. The hidden field is NOT equal to the drop down. To restate what I was trying to do:

I have a table called LT_Category.
It has 2 fields called SCategory and SColumn.
I run a query and pull all the records from LT_Cat.
I put the SCategory results in a listbox in a form.

There are a few other form fields in the form including one called SPath.

The user fills in SPath, selects the SCategory via the listbox and inserts the form contents into a different table (tbl_Main). This new table stores: SCategory, SPath - and based on the choice the user made for SCategory - the SColumn value. I couldn't figure out how to make SColumn change value to match SCategory when the user changed the listbox. It finally hit me to just put a query in the IF statement.

I'm serious about the books I mentioned in the first post though. I'm responsible for 3 public websites and a huge intrant - including all updates, server security, taking photos, most DB design and admin (27 and counting) graphics, Flash, and ad production for the entire company, and oh ya, dynamic coding. I have no help so time available to learn to program is very limited. Any book suggestions would be really appreciated.

Cheers,

Bluetone
 

although it may not teach you tricks and hacks and stuff, that'll come in time. but it's a good book, it's what I started with. (actually the CF 5 version of the book)

do you see what I did in the second post i made? i put both values in the drop down box and made a list with the pipe "|" as a delimiter

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
BBoy,

Actually no I didn't see that (using pipe as delimiter). Your method is better - not surprisingly.

I have most of the CF books. O'Reilly, Forta, "Inside CF MX", and several others. I guess it takes time as I see constant improvement but a book that dealt with forms / unusual display methods, SQL tricks etc would be really helpful. Frequently used tricks and hacks that don't seem to be covered in any book. I know there is no substitute for expertise, but I don't have time to work on a simple problem for 8 hours. No time to reinvent the wheel once a week. It's good for my programming but hard on my employer.

By the way the "Inside ColdFusion MX" book, while not a comprehensive reference, is a great book for improving / optimizing CF code.

Cheers,

Bluetone
 
I read the "programming cf 5" book... or at least the first 150 pages of it. I learned enough to re-write a simple ASP page in CF. after that i learned a lot by living in this forum. Ecob and webmigit taught me a lot. rudy keeps me straight with sql also.

I've started 34 posts... check out some of my earlier questions, very noobish... just takes practice

We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top