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

how to populate a text box when a drop down is selected? 2

Status
Not open for further replies.

jfdabiri

MIS
Feb 27, 2007
282
US
hi,
i have this code in a page:
Code:
<td class="body" align="left">
    <SELECT NAME="modl" SIZE="1"> 
     <OPTION>Select A Model</option> 
     <OPTION>A20</option> 
     <OPTION>A20m</option> 
     <OPTION>A21</option>
there's another field on the page that's called amount.
i like to be able to populate this field based on what they select in the model drop-down. how would that work?
thanks.
 
You would do it with Javascript, *OR*, you can submit the page back to itself and populate the fields then.



Just my 2¢
-Cole's Law: Shredded cabbage

--Greg
 
You can add some javascript that does it.

Code:
<script>
function changeme(me)
{
var doItToME;
doItToMe =window.document.getElementById("someTextBox");
doItToMe.value = me.value;
}
</script>

<select name="modl" size=1 onchange="return changeme(this)">
.
.
.
</select>
<input type=text name="someTextBox" id="someTextBox">

That would put the value of whatever you selected in the drop down into the text box.

If you wanted to select something with the drop down, then populate a whole pile of other crap into a text box based on looking something up in a database, take a look at AJAX.

Similar concept. You have clientside javascript that that uses an asp page server side, returns an xml dom object and you take whatever is returned and stick it in the text box like I do above.

To build may have to be the slow laborious task of years. To destroy can simply be the thoughtless act of a single day.
 
thanks jeepxo & gbaughma
can this be done in vb script instead of java?
 
jfdabiri:

Yes.

But you end up calling the web page several times.

Here's a bit of a routine I just wrote.... take a look, it populates multiple drop-down boxes based on the choice of the last one.

Code:
<%
Dim cn, rs

Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "DSN=database;Uid=*****;Pwd=*****;"

SQLStmt = "SELECT * FROM Youths WHERE Status='active' ORDER BY YLName, YFName"

set rs = cn.Execute(SQLStmt)

 %>


<HTML>
<head>
<title>Therapeutic Consequence Entry</title>

</head>
<body>
<span style="font-size: 16pt">Therapeutic Consequence Entry System<br />
</span>

<form method="post" name="Form1">
    <table>
        <tr>
            <td style="width: 225px;">
    Select Youth --&gt;</td>
            <td style="width: 310px;">
<select name="YouthID" onchange="Form1.action='default.asp';Form1.submit();">
<option value="0">Select Youth....</option>
<%

Do Until rs.EOF
Response.Write "<option value='" & rs("YouthID") & "'"

If Trim(Request.Form("YouthID")) = Trim(rs("YouthID")) Then
    Response.Write " selected"
End If

Response.Write ">" & rs("YLName") & ",&nbsp;" & rs("YFName") & "</option>"
rs.MoveNext
Loop

 %>
</select>
        </TD>
            <td style="width: 508px; height: 31px">
                Behavior change wanted (will show up on sanction)</td>
        </TR><tr>
            <td style="width: 225px">
                Select Category --&gt;
            </td>
            <td style="width: 310px">

<select name="category" onchange="Form1.action='default.asp';Form1.submit();">
<option value="0">Select Category...</option>
<% 
cn.Close
cn.Open "DSN=Logging; Uid=*****;Pwd=*****;"
SQLStmt = "SELECT DISTINCT Category FROM Cons_Lookup ORDER BY Category; "

Set rs = cn.Execute(SQLStmt)

Do Until rs.EOF
Response.Write "<option value='" & rs("Category") & "'"
If Request.Form("category") = rs("Category") Then
    Response.Write " selected"
End If

Response.Write ">" & rs("Category") & "</option>"
rs.MoveNext
Loop

cn.Close
 %>
</select>
            </td>
          <td style="width: 508px" rowspan="3" valign="top">
              &nbsp;<textarea name="tbehave" id="tbehave" style="width: 463px; height: 157px">
          </textarea></td>
        </tr>
        <tr>
            <td style="width: 225px">
                Select Violation --&gt;</td><td style="width: 310px">
                <select name="Violation" onchange="Form1.action='default.asp';Form1.submit();">

<%
If Request.Form("Category") <> "" AND Request.Form("Category") <> "0" Then
Response.Write "<option value='0'>Select Violation...</option>"
' populate the next portion
cn.Open "DSN=LoggingSQL; Uid=sa;Pwd=;"
SQLStmt = "SELECT * FROM Cons_Lookup WHERE Category='" & Request.Form("Category") & "' ORDER BY Category; "
Set rs = cn.Execute(SQLStmt)

Do Until rs.EOF
Response.Write "<option value='" & rs("Violation") & "'"
If Request.Form("Violation") = rs("Violation") Then
    Response.Write " selected"
    iViolationLevel = rs("Ranking")
    setFlag = 1
    Else
    setFlag = 0
End If
Response.Write ">" & rs("Violation") & "</option>"
If setFlag = 1 Then
    strText = Trim(rs("BehaviorChange"))
   	strText = replace(strText, chr(39), "`")	' Eliminate Single apostrophe
    	strText = replace(strText, chr(34), "`")	' Eliminate Double Quote Sign
    	strText = replace(strText, chr(145), "`")	' Eliminate OPEN Single Quote Sign
    	strText = replace(strText, chr(146), "`")	' Eliminate CLOSE Single Quote Sign
    	strText = replace(strText, chr(147), "`")	' Eliminate OPEN Double Quote Sign
    	strText = replace(strText, chr(148), "`")	' Eliminate CLOSE Double Quote Sign 
    Response.Write "<script language='Javascript'>Form1.tbehave.value='" & strText & "';</script>"
    setFlag = 0
End If
rs.MoveNext
Loop
rs.close
Else
setFlag = 0
Response.Write "<option value='0'>Select Category First...</option>"
End If


%>
            </select></td>
        </tr>
        <tr>
            <td style="width: 225px; height: 115px;" valign="top">
            Select Consequence --&gt;
            </td>
            <td style="width: 310px; height: 115px;" valign="top">
            <select name="Consequence">

<%
' First, put up the RECOMMENDED consequences for the action
If Request.Form("Violation") <> "" AND Request.Form("Violation") <> "0" Then
    Response.Write "<option value='0'>Select Consequence...</option>"
    SQLStmt = "SELECT * FROM Cons_Lookup WHERE Category='" & Request.Form("Category") & "' AND Violation='" & Request.Form("Violation") & "'; "
    Set rs = cn.Execute(SQLStmt)
    If rs.BOF AND rs.EOF Then
        Response.Write "No Consequences found for category " & Category & ".  Please call MIS."
        Response.End
    Else
    ' Put these consequences first... there may be up to 4... but there will always be at least one...
    Response.Write "<optgroup label='Recommended Consequences'>"
    Response.Write "<option>" & rs("Recommend1") & "</option>"
    If rs("Recommend2") <> "" Then
        Response.Write "<option>" & rs("Recommend2") & "</option>"
    End If
    If rs("Recommend3") <> "" Then
        Response.Write "<option>" & rs("Recommend3") & "</option>"
    End If
    If rs("Recommend4") <> "" Then
        Response.Write "<option>" & rs("Recommend4") & "</option>"
    End If
    End If

    ' We put up the recommended ones.... now put up the others in the same violation level....
    Response.Write "</optgroup><optgroup label='Other Consequences'>"
    If Request.Form("Repeated") = "on" Then
    SQLStmt = "SELECT DISTINCT Recommend1 FROM Cons_Lookup WHERE (Ranking <= '" & iViolationLevel + 1 & "' AND Category='" & Request.Form("Category") & "') ORDER BY Recommend1; "
    Else
    SQLStmt = "SELECT DISTINCT Recommend1 FROM Cons_Lookup WHERE (Ranking='" & iViolationLevel & "' AND Category='" & Request.Form("Category") & "') ORDER BY Recommend1; "
    End If
    Set rs = cn.Execute(SQLStmt)
    Do while not rs.EOF
            Response.Write "<option>" & rs("Recommend1") & "</option>"
    'If rs("Recommend2") <> "" Then
    '    Response.Write "<option>" & rs("Recommend2") & "</option>"
    'End If
    'If rs("Recommend3") <> "" Then
    '    Response.Write "<option>" & rs("Recommend3") & "</option>"
    'End If
    'If rs("Recommend4") <> "" Then
    '    Response.Write "<option>" & rs("Recommend4") & "</option>"
    'End If
    rs.MoveNext
    Loop
    rs.close
    Response.Write "<option>Consequence from Treatment Plan</option></optgroup>"
    
Else
    Response.Write "<option value='0'>Select Violation first...</option>"
    
End If
 %>            
            
            </select>
                <br />Therapeutic Consequence Entry:<br />
                <textarea name="other" cols=30 rows=4></textarea>     
        </td>
        </tr>
        <tr>
            <td style="width: 225px; height: 115px" valign="top">
                Staff Name: <%=Session("UserID") %>
            </td>
            <td style="width: 310px; height: 115px" valign="top"><button type="button" OnClick="Form1.action='postcons.asp';Form1.submit();" >Click once to submit!</button>
            </td>
            <td rowspan="1" style="width: 508px" valign="top">
            <%
            Response.Write "<input type='checkbox' name='repeated' "
            If Request.Form("repeated") = "on" Then
                Response.Write "checked "
            End If
            Response.Write "onclick=""Form1.action='default.asp';Form1.submit();"" /><-- Check here if repeated behavior (this will increase the severity of the consequences, but also alert a supervisor)"
            %>
            </td>
        </tr>
        </table>


</form>
</body>
</HTML>

As you can see, the above code has several different things going on.
1) It has a session variable for the user name
2) Each time you change a drop-down, the page calls itself, and re-reads the database, populating the next drop-down box.

This code Copyright (c) 2008 Greg Baughman



Just my 2¢
-Cole's Law: Shredded cabbage

--Greg
 
OH MY GOD,
good grief. i think java is the way to go. i just have to bite the bullet and learn it. the brief code that was posted by jeepxo, did the trick very nicely. thanks for all the trouble. my routine works very well now. i just used a series of "if" statements to build the logic.
thanks a whole bunch.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top