PDA

View Full Version : auto refresh of textarea based on drop down menu


bean262
08-12-2003, 08:35 PM
Hello all,

I am working with an asp page that dynamically pulls info from my data base to populate a drop-down menu and text area, where I can make changes and update the database.

Currently the drop-down menu is populated dynamically from the database, as is the text area (with the first record on the record set).

But, what I would like to have happen is:
When I change the option on the drop-down menu, I want it to pull the data associated with that option in the database to be displayed in the textarea.

I am thinking that it would require some JavaScript, but I am not sure. Any help would be greatly appreciated!!

glenngv
08-13-2003, 05:34 AM
store the select and textarea data in a Javascript associative array

var arr = [
["option1","text1"],
["option2","text2"],
["option3","text3"],
];

...

<select name="sel" onchange="this.form.desc.value=arr[this.selectedIndex][1]">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</select>
<textarea name="desc">text1</textarea>

the code in blue must be generated by ASP/VBScript

or you don't even need to store the option data in the array, just the textarea data.

var arr = ["text1","text2","text3"];

...

<select name="sel" onchange="this.form.desc.value=arr[this.selectedIndex]">
<option value="option1">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</select>
<textarea name="desc">text1</textarea>

bean262
08-13-2003, 03:41 PM
Glenn, thanks!

Question though...

My text box is generated dynamically also from the same table. Currently my code is as follows:

<select name="role_id" onChange="this.form.message.value=arr[this.selectedIndex]">
<%
dim objRS
Set objRS = CreateObject("ADODB.Recordset")
sql="select role_name as ROLENAME, role_id as ROLEID from TABLE_NAME"
objRS.Open sql, conn
While Not objRS.EOF
%>
<option value="<%=objRS("ROLEID")%>"><%=objRS("ROLENAME")%>
<%
objRS.MoveNext
Wend
set objRS = Nothing
%>
</select>

Code in Red is what pulls the dynamic drop-down menu.

The code below in BLUE is supposed to pull the data for textarea...


<%
dim objRS2
Set objRS2 = CreateObject("ADODB.Recordset")
sql="select role_id as ROLEID, announcement as MESSAGE from TABLE_NAME "
objRS2.Open sql, conn
%>
<textarea name="message" cols="50" rows="5" wrap="VIRTUAL"><%=objRS2("MESSAGE")%></textarea>

When I hit submit, it changes the ANNOUNCEMENT field in the database.

Again, when I would like the page to function so that when I select a different "ROLE" from the drop-down menu it changes populates the Text in the textarea.

I am a complete newbie at ASP and know nothing about JavaScript. Any additional help, would be greatly appreciated...I think the var array thing will work, but i have not a clue how to code that to pull dynamically.

THANKS for putting up with me!!

--Steve

khillabolt
08-13-2003, 11:52 PM
Greetings,

Well, if you're looking to have this text box populate without have to reload the page, then I'm afraid you'll need to look in remote scripting. This little tool has become a favorite of mine over the years, as most users don't like have the page reload.

http://www.microsoft.com/mind/0100/cutting/cutting0100.asp

It gets a little tricky to make it work, but the examples show exactly how it's done.

If you have any questions, please feel free to contact me, here or offline.

Hope this helps...

bean262
08-17-2003, 08:04 PM
I don't really care if the page reloads or not. I was thinking that it should be easier to have it refresh that text box than it would be to have it reload the page with the variable from the drop-down.

I could code it all in ColdFusion, no problem. But ASP is not one of my strong points...however, I am always willing to learn!!

glenngv
08-18-2003, 03:11 AM
generating Javascript codes in ASP is no different with generating HTML.

if you can response.write("<option value=""" & objRS("ROLEID") & """>" & objRS("ROLENAME") & "</option>")

you can also response.write("var arr = new Array();")

as long as it's between <script> tags.

bean262
08-18-2003, 03:18 AM
ok...

I guessing I am just unsure as to how to right the var code to pull from the database...i guess i just have to play around with it some...

Thanks!