Question - ASP + Ajax

Hey!

Not sure if this is the correct section to post this. ;)

Well, I have a table in my sql server 2000 database

***************
tblAddress
----------------------------
City_Name (varchar)
Venue_Name (varchar)
*******************

The "City_Name" column contains all the name of the cities in my country and each city has multiple venues.

Now I have 2 drop-down boxes in my asp page. The first drop-down is already listed with all the cities which are in the "City_Name" column.

What I now want is, when I select a city name from the first drop-down, the second drop down should be populated with all the venue names of that city (of course without refreshing the page)

I know this can be done with ajax.

I got few codes while googling but none of them are working :(

Recommended Answers

All 2 Replies

You need to query the database using ajax get or post method, and then use callback to show the second dropdown menu using innerHTML.

This is the code got, it works fine if I place a textbox instead of teh second drop-down.

main.asp file

<script type="text/javascript">
<!--
function myfunction()
{
var xmlhttp = false;
//Check if we are using IE.
try {
//If the Javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
alert ("You are using Microsoft Internet Explorer.");
} catch (e) {
//If not, then use the older active x object.
try {
//If we are using Internet Explorer.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
alert ("You are using Microsoft Internet Explorer");
} catch (E) {
//Else we must be using a non-IE browser.
xmlhttp = false;
}
}
//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
abc = document.getElementById('txtname').value;
var obj = document.getElementById('txtshouvik');
xmlhttp.open("GET", "execute.asp?str="+  abc);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.value = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
//-->
</script>
<select name="txtCity" id="txtCity" onchange="javascript:myfunction();">
				<%
				set rsCity = conn.execute("SELECT DISTINCT Venue_City from PRS_Master_Venue")
				do until rsCity.eof
				%>
				<option onclick="javascript:myfunction();"><%=rsCity("Venue_City")%></option>
		
				<%
				rsCity.movenext
				loop
				%>
                </select>
				
<select name="txtCity" id="txtCity" onchange="javascript:myfunction();">
<option onclick="javascript:myfunction();"><%=rsCity("Venue_City")%></option>
</select>
<input name="btn" type="button" value="click" onClick="javascript:myfunction();"/>

execute.asp file

<%
response.Write "Venue Name here..."
%>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.