...

View Full Version : Populating a combo based on another combo



fidodido
01-25-2005, 03:45 AM
I want to populate a combo box that depends on the selection of another combo box.
for example, from the first combo i select any country name, the second combo automatically fills the city names for that country from the database.

glenngv
01-25-2005, 05:29 AM
Do you want the page not to refresh when a selection is made? If yes, then you need a javascript solution, otherwise, you need an ASP solution. If you want the former, you can find several scripts out there. But even then, you still need ASP to generate all the options. Just google for "double combo javascript".

fidodido
01-26-2005, 03:22 AM
Yes i want the to refresh. I've wrote the following code but the problem is that the first combo always returns the first value which is also at the top in the DB. How it can be keep tracked that when pages refreshes it holds the selected value.
<html>
<head>
<title>Untitled Document</title>
<script>
function doSubmit()
{
document.fSelect.action = "combo.asp"
document.fSelect.method ="post"
document.fSelect.submit();
}
</script>
</head>

<body>
<%
set con = Server.CreateObject("ADODB.Connection")
set Rs = Server.CreateObject("ADODB.Recordset")

Con.Open strConnect
'Rs.Open "CustomerInfo", Con, 1, 2

SQL = "Select * FROM City"
Rs.Open SQL, Con, 1, 2
%>
<form name="fSelect" method="post" action="comboResult.asp">
<p> city
<select name="City" id="city" onchange="doSubmit()">

<% While Not Rs.EOF %>
<option value= <% = Rs.Fields("SNo")%>><% =Rs.Fields("City")%></option>
<%

Rs.MoveNext
Wend

%>
</select>
</p>
<%
'response.write (Session.value("val"))
If Request.Form("City") > 0 Then

'strSQl= "select * from Towns where SNo=" & Request.Form("Town")
strSQl= "select * from Towns where SNo=SNo"
Rs.close
Rs.open strsql, con

End If

%>

<p>town
<select name="town" id="town">
<% If Rs.RecordCount > 0 Then
While Not Rs.EOF %>
<option value=<% =Rs.Fields("Towns")%>><% =Rs.Fields("Towns")%></option>
<% Rs.Movenext
Wend
End if
%>
</select>
</p>
<p>&nbsp;</p>
<input type=submit>
</form>
</body>
</html>

glenngv
01-26-2005, 04:03 AM
<html>
<head>
<title>Untitled Document</title>
<script>
function doSubmit(f)
{
f.action = "combo.asp";
f.method = "post";
f.submit();
}
</script>
</head>

<body>
<%
set con = Server.CreateObject("ADODB.Connection")
set Rs = Server.CreateObject("ADODB.Recordset")

Con.Open strConnect
'Rs.Open "CustomerInfo", Con, 1, 2

SQL = "Select * FROM City"
Rs.Open SQL, Con, 1, 2

dim selectedCity, rsNo, selected
selectedCity = request.form("City")
%>
<form name="fSelect" method="post" action="comboResult.asp">
<p> city
<select name="City" id="city" onchange="doSubmit(this.form)">

<%
While Not Rs.EOF
rsNo = Rs.Fields("SNo")
if selectedCity = rsNo then selected = " selected=""selected""" else selected=""
%>
<option value="<% = rsNo%>"<% = selected%>><% =Rs.Fields("City")%></option>
<%
Rs.MoveNext
Wend
%>
</select>
...

fidodido
01-28-2005, 06:35 PM
its not working ... the problem is same, page refreshes after the selection but again shows the first value of the DB.

glenngv
01-31-2005, 01:07 AM
Do you have the page online?

fidodido
01-31-2005, 07:00 PM
No, i don't hav the page online, i'm using it at local host.
I have pasted the code above but if u need i'll upload it.
Can i have you messenger id so can talk u directly?

WA
01-31-2005, 07:10 PM
At some point you will need a JavaScript to actually create the inter-dependant selects. For that check out Chained Selects: http://www.dynamicdrive.com/dynamicindex16/chainedselects/

glenngv
02-01-2005, 02:22 AM
No, i don't hav the page online, i'm using it at local host.
I have pasted the code above but if u need i'll upload it.
Can i have you messenger id so can talk u directly?
Can you post the generated HTML code for the combobox?

fidodido
02-02-2005, 02:06 PM
<!-- #include file=../db/Connect.asp -->

<html>
<head>
<title>Untitled Document</title>

<script>
function doSubmit()
{
document.fSelect.action = "comb.asp"
document.fSelect.method ="post"
document.fSelect.submit();
}
</script>

</head>

<body>
<%
set con = Server.CreateObject("ADODB.Connection")
set Rs = Server.CreateObject("ADODB.Recordset")

Con.Open strConnect

SQL = "Select * FROM City"
Rs.Open SQL, Con, 1, 2
%>
<form name="fSelect">
<p> city
<select name="City" id="city" onchange="doSubmit()">

<% While Not Rs.EOF %>
<option value= <% = Rs.Fields("SNo")%>><% =Rs.Fields("City")%></option>
<%

Rs.MoveNext
Wend

%>
</select>

<P>
<input type=text name=focus value="<%= Request.Form("City") %>">
<%

If Request.Form("City") > 0 Then
Rs.close
strSQl= "select * from Towns where SNo=" & Request.Form("focus")
'strSQl= "select * from Towns where SNo=SNo"

Rs.open strSQL, con

End If

%>

<p>town
<select name="town" id="town">
<% If Rs.RecordCount > 0 Then
While Not Rs.EOF %>
<option value=<% =Rs.Fields("Towns")%>><% =Rs.Fields("Towns")%></option>
<% Rs.Movenext
Wend
End if
%>
</select>

</form>
</body>
</html>

glenngv
02-03-2005, 03:31 AM
You did not incorporate the changes I made. :rolleyes:

fidodido
02-04-2005, 10:23 AM
Sorry, the above code is pasted wrong. But your code is doing the same.
Select any city name from the list, then page refreshes and shows the first value which is also first in the DB. It didn't stores the user's selected value.
Can i send u the file so that u may view it correctly.

glenngv
02-07-2005, 02:05 AM
Is the page submitting to itself when onchange occurs in the combo box?
You must specify the current page in the action attribute.


function doSubmit(f)
{
f.action = "combo.asp"; //must be the current page
f.method = "post";
f.submit();
}

fidodido
02-07-2005, 07:07 PM
yes its written correctly, i've checked it. Page refreshes after the selection from first combo but didn't stores the selected value and always shows the first value.

glenngv
02-08-2005, 05:28 AM
It's hard if we can't see the page in action.

fidodido
02-11-2005, 02:56 AM
Can i send you the file on ur ID so u may have closer look?

miranda
02-11-2005, 08:40 PM
to get the original combo box to point to the value selected you need to write an if statement that checks to see if the value was selected is equal to the value of that selection.
Changes are made in Red


<!-- #include file=../db/Connect.asp -->

<html>
<head>
<title>Untitled Document</title>

<script>
function doSubmit()
{
document.fSelect.action = "comb.asp"
document.fSelect.method ="post"
document.fSelect.submit();
}
</script>

</head>

<body>
<%
Dim City
City = Request.Form("city")
set con = Server.CreateObject("ADODB.Connection")
set Rs = Server.CreateObject("ADODB.Recordset")

Con.Open strConnect

SQL = "Select * FROM City"
Rs.Open SQL, Con, 1, 2
%>
<form name="fSelect">
<p> city
<select name="City" id="city" onchange="doSubmit()">

<select name="City" id="city" onchange="doSubmit()">

<% While Not Rs.EOF
Response.Write "<option value=""" & Rs.Fields("SNo") & """
If City = Rs.Fields("City") Then Response.Write " Selected"
Response.Write ">Rs.Fields("City")</option>" & vbCrLf
Rs.MoveNext
Wend
%>
</select>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum