ASP에서 Ajax 사용 예제

Posted at 2007.10.19 11:42 | Posted in 프로그래밍/ASP, Javascript


zip_search.asp 소스 코드
======================================================================================
<%
response.charset = "euc-kr"
strConnect="디비 문자열 스트링"
set dbcon = server.CreateObject("ADODB.Connection")
dbCon.Open strConnect
sql = "select distinct sido from dbo.zip_table order by sido"
set rs = dbCon.Execute(sql)
%>
<script language="javascript">
<!--
var xmlHttp;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
 xmlHttp = new XMLHttpRequest();    
    }
}
/*********************************************************
*****  시도 선택시  활성화 되는 폼    *****
*********************************************************/
function get_gugun_list(sido)
{
 createXMLHttpRequest();
 xmlHttp.onreadystatechange = get_gugun_list_callback;
 xmlHttp.open("GET","zip_gugunlist.asp?sido="+escape(sido), true);
 xmlHttp.send();
}
function get_gugun_list_callback()
{
 if (xmlHttp.readyState == 4) {
  if (xmlHttp.status == 200) {
   if (typeof(document.all.div_gugun) == "object") {
    document.all.div_gugun.innerHTML = xmlHttp.responseText;
   }
  }
 }
}
function get_addr_list()
{
 var sido, gugun, dong;
 sido  = document.frmzip.sido.value;
 gugun = document.frmzip.gugun.value;
 dong  = document.frmzip.dong.value;
 if (dong.length < 2) return false;
 createXMLHttpRequest();
 xmlHttp.onreadystatechange = get_addr_list_callback;
 xmlHttp.open("GET", "zip_addrlist.asp?sido=" + escape(sido) + "&gugun=" + escape(gugun) + "&dong=" + escape(dong), true);
 xmlHttp.send();
}
function get_addr_list_callback()
{
 if (xmlHttp.readyState == 4) {
  if (xmlHttp.status == 200) {
   if (typeof(document.all.div_zip_list) == "object") {
    document.all.div_zip_list.style.display = "";
    document.all.div_zip_list.innerHTML = xmlHttp.responseText;
   }
   else
    document.all.div_zip_list.style.display = "none";
  }
 }
}
//-->
</script>
<style>
 input, select, td { font-famliy: tahoma; font-size:9pt}
</style>
<table border="0" width="400">
<form name="frmzip">
 <tr>
  <td>
  <select name="sido" omChange="get_gugun_list(this.value);">
   <option value="">시도선택</option>
   <%
    Do While Not rs.Eof
   %>
     <option value="<%=rs("sido")%>"><%=rs("sido")%></option>
   <%
    rs.MoveNext
    Loop
    rs.close
    set rs = nothing
   %>
  </select>
  </td>
  <td>
  <div id="div_gugun">
   <select name="gugun" >
    <option value="">구/군선택</option>
   </select>
  </div>
  </td>
  <td>
   동이름 : <input type="text" name="dong" style="width:50">
   <input type="button" value="검색" omClick="get_addr_list();" style="width:40;border-style:solid;border-width:1">
  </td>
 </tr>
 <tr>
  <td colspan="3" align="center">
  동이름을 입력하시오(2글자 이상)<br>(예:논현, 구의1동, ...)
  </td>
 </tr>
</form>
</table>
<div id="div_zip_list" style="dislplay:none;overflow-y:scroll; height:300; width:400">
 
 
zip_gugunlist.asp 소스 코드
======================================================================================
<%
response.charset = "euc-kr"
sido = request("sido")
strConnect="디비 연결 스트링"
set dbcon = server.CreateObject("ADODB.Connection")
dbCon.Open strConnect
sql = "select distinct gugun from dbo.zip_table where sido = '" & sido & "' order by gugun"
set rs = dbCon.Execute(sql)
%>
<select name="gugun">
 <option value="">구/군선택</option>
 <%
  Do While Not rs.Eof
 %>
   <option value="<%=rs("gugun")%>"><%=rs("gugun")%></option>
 <%
  rs.MoveNext
  Loop
  rs.close
  set rs = nothing
 %>
</select>

zip_addrlist.asp 소스 코드
======================================================================================
<%
response.charset = "euc-kr"
sido  = request("sido")
gugun = request("gugun")
dong  = request("dong")
strConnect="디비 문자 연결 스트링"
set dbcon = server.CreateObject("ADODB.Connection")
dbCon.Open strConnect
if sido = "" and gugun = "" Then
 sql = "select zipcode, sido + ' ' + gugun + ' ' + dong + ' ' + isnull(bunji, '') as addr from dbo.zip_table where dong like '%" & dong & "%'"
elseif sido <> "" and gugun = "" Then
 sql = "select zipcode, sido + ' ' + gugun + ' ' + dong + ' ' + isnull(bunji, '') as addr from dbo.zip_table where sido = '" & sido & "' and dong like '%" & dong & "%'"
elseif sido <> "" and gugun <> "" Then
 sql = "select zipcode, sido + ' ' + gugun + ' ' + dong + ' ' + isnull(bunji, '') as addr from dbo.zip_table where sido = '" & sido & "' and gugun = '" & gugun & "' and dong like '%" & dong & "%'"
end if
'response.write sql
set rs = dbCon.Execute(sql)
%>
<table cellpadding="3" cellspacing="3" border="1" width="400">
<%
 Do While Not rs.Eof
'  addr = rs("sido") & " " & rs("gugun") & " " & rs("dong") & " " & rs("bunji")
%>
 <tr>
  <td width="100">
   <%=rs("zipcode")%>
  </td>
  <td>
   <%=rs("addr")%>
  </td>
 </tr>
<%
 rs.MoveNext
 Loop
 rs.close
 set rs = nothing
%>
</table>



Name __

Password __

Link (Your Website)

Comment

SECRET | 비밀글로 남기기