googleAdsense_widever


ajax php 통신 simple example PHP

<client.html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> Hello Fucking Web </title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <style type="text/css">
  div.elem{margin:20px;}
  </style>

  <script type="text/javascript">
  //<![CDATA[ CDATA에 대해서는 http://kwon37xi.egloos.com/3798259 참조

//XMLHttpRequest 객체 생성 부분
  var xmlhttp=false;
  if(window.XMLHttpRequest){
  xmlhttp=new XMLHttpRequest();
  xmlhttp.overrideMimeType('text/xml');
  }
  else if (window.ActiveXObject){
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  function populateList(){
 var state=document.forms[0].elements[0].value;
 var url='server.php?state='+state; // get method의 전송이므로
 xmlhttp.open('GET',url,true); //xhr 연결 설정 http://harrys.co.kr/blog/lab/118 참조
 xmlhttp.onreadystatechange=getCities;
 xmlhttp.send(null);
  }
  
  function getCities(){
 if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { //통신여부체크
 document.getElementById('cities').innerHTML = "<select>"+xmlhttp.responseText+"</select>";
 }else{
 document.getElementById('cities').innerHTML = 'Error:preSearch Failed!';
 }
  }
  //]]>
  </script>
  </head>
 <body>
  
  <h3>Select State:</h3>
  <form action="server.php" method="get">
  <div class="elem">
//드롭박스
  <select onChange="populateList()"> //populateList() 호출
  <option value="CA">California</option>
  <option value="MO">Missouri</option>
  <option value="WA">Washington</option>
  <option value="ID">Idaho</option>
  </select>
  </div>
  
  <h3>Cities:</h3>
  <div class="elem" id="cities">
  <select></select>
  </div>
  </form>
 </body>
</html>

<server.php>
<?

// 검색 문자열을 넘겨받았는지 체크
if(empty($_GET['state']))
{
echo "Select State first.";
}
else {
//검색의 처음과 끝에서 공백 문자를 제거한다.
$search = trim($_GET['state']);
switch($search)
{
case "MO" : 
$result = "<option value='St.Louis'>St. Louis</option>".
"<option value='Kansas City'>Kansas City</option>";
break;
case "WA" :
$result = "<option value='Seattle'>Seattle</option>".
"<option value='Olympia'>Olympia</option>";
break;
case "CA" : 
$result = "<option value='SanFrancisco'>San Francisco</option>".
"<option value='Los Angeles'>Los Angeles</option>".
"<option value='Web 2.0 City'>Web 2.0 City</option>".
"<option value='barcamp'>BarCamp</option>";
break;
case "ID" :
$result = "<option value='Boise'>Boise</option>";
break;
default :
$result = "No Cities Found";
break;
}
echo $result;
}
?>

덧글

댓글 입력 영역


공지

어서오십시오.
트위터 : @FCliver
기저심리학 : 네이버카페
카카오톡 : FCliver
페이스북 : Fredric Cliver

통계 위젯 (화이트)

1420
105
286340

접속자 위치