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

Updating a field on the fly

Status
Not open for further replies.

n0795

Programmer
Jul 30, 2001
136
US
Below is some code i use to get a list of categories from a database, Into a drop down box.
At the bottom i have just a text field called "title".
How can i update the field "title" with whe data that is selected in the catergory field
These two fields need to be the same but i done want to make the user type it in.
any help would be great....

<select name=&quot;category&quot;>
<%
dim objRS

set objrs=objconn.execute(&quot;select * from categories&quot;)

if objrs.eof then
%>
<script>
window.open('add_category.asp','Add','toolbar=no,width=450,height=200,menubar=no,resize=no')
</script>
<%
end if

do while not objrs.eof

'if just added make selected
if request.querystring(&quot;cat&quot;) = objrs(&quot;category&quot;) then
response.write &quot;<option selected value='&quot; & objrs(0) & &quot;'>&quot; & objrs(&quot;category&quot;)
else

response.write &quot;<option value='&quot; & objrs(0) & &quot;'>&quot; & objrs(&quot;category&quot;)

end if

objrs.movenext

loop

set objrs = nothing
set objconn = nothing
%>
</select>
<%
'added category notice
if len(request.querystring(&quot;cat&quot;)) > 0 then
Response.write &quot;<font color='&quot; & actioncolor & &quot;'>Title Added</font>&quot;
end if
if request.querystring(&quot;added&quot;) = &quot;link&quot; then
Response.write &quot;<font color='&quot; & actioncolor & &quot;'>Link Added</font>&quot;
end if
%>



------------
<input size=&quot;50&quot; name=&quot;title&quot; value=&quot;&quot;>
 
Write a javascript function that places the value of the select element in the list box into the title component. Call this method in the list box's onChange() method.
Code:
<form name=&quot;mainForm&quot;>
<select name=&quot;categoryBox&quot; onChange=&quot;javascript:populateTitle()&quot;>
... blah blah blah ...
</select>
<input type=&quot;text&quot; size=&quot;50&quot; name=&quot;title&quot; value=&quot;&quot;>
</form>
[code]

Javascript code:
[code]
<script language=&quot;javascript&quot;>
  function populateTitle() {
    var listBox = document.forms[&quot;mainForm&quot;].elements[&quot;categoryBox&quot;];
    var titleText = document.forms[&quot;mainForm&quot;].elements[&quot;title&quot;];
    for(i=0; i<listBox.options.length; i++) {
       if (listBox.options[i].selected) {
          titleText.value = listBox.options[i].value;
       }
    }	      
  }
</script>

I didn't test this but it should work fine. Wushutwist
 
I have run into a problem this code does work but instead of displaying what shows up in the dropdown box its shows the catergories ID number in the title field.
It gets this ID number from the database.
I may be able to work with this and change how its shows up on the web page but any help would be great.
Thanks

<form name=&quot;add&quot; action=&quot;addlink.asp&quot; method=&quot;post&quot; onsubmit=&quot;return validate(this)&quot;>

<select name=&quot;category&quot; onChange=&quot;javascript:populateTitle()&quot;>

<script language=&quot;javascript&quot;>
function populateTitle() {
var listBox = document.forms[&quot;add&quot;].elements[&quot;category&quot;];
var titleText = document.forms[&quot;add&quot;].elements[&quot;title&quot;];
for(i=0; i<listBox.options.length; i++) {
if (listBox.options.selected) {
titleText.value = listBox.options.value;
}
}
}
</script>

<input size=&quot;50&quot; name=&quot;title&quot; value=&quot;&quot;>
 
It displays whatever the value you are setting the elements in the list to be. You can't do much if you set the elements to be the ids, unless you initialize a javascript array when the page loads and you lookup the name of the item using the id you get from the list. It is pretty simple code. I will leave that exercise to you.
Code:
<script language=&quot;javascript&quot;>
  function populateTitle() {
    var listBox = document.forms[&quot;mainForm&quot;].elements[&quot;categoryBox&quot;];
    var titleText = document.forms[&quot;mainForm&quot;].elements[&quot;title&quot;];
    for(i=0; i<listBox.options.length; i++) {
       if (listBox.options[i].selected) {
          titleText.value = lookup(listBox.options[i].value);
       }
    }          
  }

  function lookup(listID) {
  }
</script>

You need to write lookup and dynamically create the javascript array in ASP. Sounds harder than it actually is. Let me know if you have any trouble. Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top