|
ÀüÅëÀûÀÎ À¥ ÇÁ·Î±×·¥ÀÇ °æ¿ì Ãß°¡ ÇÁ·Î¼¼½º´Â ´ÙÀ½°ú °°ÀÌ ÁøÇàµÈ´Ù.
1. Àüü ¸®½ºÆ®¸¦ Á¶È¸Çϴ ȸ鿡¼ Ãß°¡ ¹öư Ŭ¸¯
2. Ãß°¡ÇÒ Ç׸ñÀÇ ¼¼ºÎ»çÇ×À» ÀԷ¹ÞÀ» ȸé
3. ÀÔ·ÂÀ» ¸¶Ä¡¸é ÀúÀå ¹öư
4. ÀÔ·ÂÀÌ Á¤»óÀûÀ¸·Î ³¡³µ´Ù´Â ȸé ȤÀº ¸Ç óÀ½ Àüü ¸®½ºÆ® ȸé.
À̰Íó·³ Ãß°¡ ÇÁ·Î¼¼½º¸¦ ±¸ÇöÇϱâ À§Çؼ Ç׸ñÀ» ÀԷ¹޴ ÆäÀÌÁö¸¦ Ãß°¡·Î ±¸ÇöÇØ¾ß ÇÑ´Ù. ÇÏÁö¸¸ ajax ¸¦ ÀÌ¿ëÇÏ¿© ÇÑ ÆäÀÌÁö ³»¿¡¼ ¸ðµç °ÍÀ» ´Ù ±¸ÇöÇÒ ¼ö ÀÖ´Ù. Ãß°¡ ÇÁ·Î¼¼½º°¡ ÁøÇàµÇ°í ³ ÈÄ ÇÊ¿äÇÑ ºÎºÐ¸¸ refresh µÇ´Â °Ç ´ç¿¬ÇÏ´Ù. ajax ¸¦ ÀÌ¿ëÇØ¼ Ãß°¡·ÎÁ÷¸¸ ±¸ÇöÇØº» ¿¹Á¦¸¦ »ìÆìº¸ÀÚ.
±×·³ ÇØ´ç ¼Ò½ºÄڵ带 »ìÆìº¸ÀÚ.
[»ùÇñ¸Çöº¸±â]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Employee List</title>
<script type="text/javascript">
var xmlHttp;
var name;
var title;
var department;
var deleteID;
var EMP_PREFIX = "emp-";
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function addEmployee() {
name = document.getElementById("name").value;
title = document.getElementById("title").value;
department = document.getElementById("dept").value;
action = "add";
if(name == "" || title == "" || department == "") {
return;
}
var url = "example14.php?"
+ createAddQueryString(name, title, department, "add")
+ "&ts=" + new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleAddStateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function createAddQueryString(name, title, department, action) {
var queryString = "name=" + name
+ "&title=" + title
+ "&department=" + department
+ "&action=" + action;
return queryString;
}
function handleAddStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
updateEmployeeList();
clearInputBoxes();
}
else {
alert("Error while adding employee.");
}
}
}
function clearInputBoxes() {
document.getElementById("name").value = "";
document.getElementById("title").value = "";
document.getElementById("dept").value = "";
}
function deleteEmployee(id) {
deleteID = id;
var url = "example14.php?"
+ "action=delete"
+ "&id=" + id
+ "&ts=" + new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleDeleteStateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
function updateEmployeeList() {
var responseXML = xmlHttp.responseXML;
var status = responseXML.getElementsByTagName("status").item(0).firstChild.nodeValue;
status = parseInt(status);
if(status != 1) {
return;
}
var row = document.createElement("tr");
var uniqueID = responseXML.getElementsByTagName("uniqueID")[0].firstChild.nodeValue;
row.setAttribute("id", EMP_PREFIX + uniqueID);
row.appendChild(createCellWithText(name));
row.appendChild(createCellWithText(title));
row.appendChild(createCellWithText(department));
var deleteButton = document.createElement("input");
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "Delete");
deleteButton.onclick = function () { deleteEmployee(uniqueID); };
cell = document.createElement("td");
cell.appendChild(deleteButton);
row.appendChild(cell);
document.getElementById("employeeList").appendChild(row);
updateEmployeeListVisibility();
}
function createCellWithText(text) {
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(text));
return cell;
}
function handleDeleteStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
deleteEmployeeFromList();
}
else {
alert("Error while deleting employee.");
}
}
}
function deleteEmployeeFromList() {
var status = xmlHttp.responseXML.getElementsByTagName("status").item(0).firstChild.nodeValue;
status = parseInt(status);
if(status != 1) {
return;
}
var rowToDelete = document.getElementById(EMP_PREFIX + deleteID);
var employeeList = document.getElementById("employeeList");
employeeList.removeChild(rowToDelete);
updateEmployeeListVisibility();
}
function updateEmployeeListVisibility() {
var employeeList = document.getElementById("employeeList");
if(employeeList.childNodes.length > 0) {
document.getElementById("employeeListSpan").style.display = "";
}
else {
document.getElementById("employeeListSpan").style.display = "none";
}
}
</script>
</head>
<body>
<h1>Employee List</h1>
<form action="#">
<table width="80%" border="0">
<tr>
<td>Name: <input type="text" id="name"/></td>
<td>Title: <input type="text" id="title"/></td>
<td>Department: <input type="text" id="dept"/></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="button" value="Add" onclick="addEmployee();"/>
</td>
</tr>
</table>
</form>
<span id="employeeListSpan" style="display:none;">
<h2>Employees:</h2>
<table border="1" width="80%">
<tbody id="employeeList"></tbody>
</table>
</span>
</body>
</html>
<example14.html ÀÇ ¸ðµç ¼Ò½º ÄÚµå>
<?
class xmlClass{
var $items = array();
var $task = "";
function outputXml(){
header("Content-type: text/xml");
printf("<?xml version=\"1.0\" encoding=\"%s\" ?>\n","EUC-KR");
print("<rss version=\"2.0\">\n");
print("<result>\n");
$this->printChannel();
print("</result>\n");
print("</rss>\n");
}
function printChannel(){
foreach($this->items as $item){
foreach ($item as $name => $value){
$value = htmlspecialchars($value);
printf("<%s>%s</%s>\n",$name,$value,$name);
}
}
}
function addItem($item){
array_push($this->items,$item);
}
function MakeUniqueKey(){
$MicroTsmp = split(" ",microtime());
return $UniqueKey = $MicroTsmp[0]+$MicroTsmp[1];
}
function makexml(){
switch ($this->action) {
case "add":
$this->addItem(array("uniqueID"=> $this->MakeUniqueKey(),"status"=> "1"));
break;
case "delete":
$this->addItem(array("status"=> "1"));
break;
default:
break;
}
$this->outputXml();
}
}
$myxml = new xmlClass();
$myxml->name = $name;
$myxml->title = $title;
$myxml->department = $department;
$myxml->action = $action;
$myxml->makexml();
?>
<example14.php ÀÇ ¸ðµç ¼Ò½º ÄÚµå>
À§ ¿¹Á¦¿¡ ´ëÇÑ ¼Ò½º¸¦ º¸¸é ½±°Ô ¾Ë°ÚÁö¸¸ Á÷¿øÀÇ Á¤º¸¸¦ ÀÔ·ÂÇϰí Ãß°¡ ¹öưÀ» ´©¸£¸é ÆÄ¶ó¹ÌÅÍ Äõ¸® ½ºÆ®¸µÀ» »ý¼ºÇؼ GET/ºñµ¿±â ¹æ½ÄÀ¸·Î ¼¹ö·Î º¸³½´Ù. ¼¹ö¿¡¼´Â ÇØ´ç Á¤º¸¸¦ DB ¿¡ insert °¡»ó ·ÎÁ÷À» ¼öÇàÇÏ°í ±× °á°ú¸¦ XML ·Î ¸¸µé¾î¼ Ŭ¶óÀÌ¾ðÆ®·Î Àü¼ÛÇÑ´Ù. XHR °´Ã¼´Â ÇØ´ç XML Á¤º¸·Î ºÎÅÍ Á¤º¸¸¦ ´Ù½Ã ÃßÃâÇϰí Å×ÀÌºí¿¡ Ãß°¡ÇÏ´Â ÇüÅÂÀÇ È帧À¸·Î Àü°³µÈ´Ù. »èÁ¦ ·ÎÁ÷µµ Ãß°¡ ·ÎÁ÷°ú °ÅÀÇ ºñ½ÁÇÏ°Ô µ¿ÀÛÇÑ´Ù.
[»ùÇñ¸Çöº¸±â]
À§ ¿¹Á¦Áß¿¡ ÇѰ¡Áö »ìÆìº¼ °ÍÀÌ ÀÖ´Ù.
deleteButton.onclick = function () { deleteEmployee(uniqueID); };
ÀÌ ºÎºÐÀÇ Äڵ带 ´ÙÀ½°ú °°ÀÌ »ç¿ëÇÒ ¼öµµ ÀÖÀ» °ÍÀÌ´Ù.
deleteButton.setAttibute("onclick", "deleteEmployee('"+uniqueID+"');");
ÇÏÁö¸¸ IE ¹× FireFox µÎ°³ÀÇ ºê¶ó¿ìÀú¿¡¼ Á¦´ë·Î ÀÛµ¿À» ÇÏÁö ¾Ê¾Ò´Ù. ÇÏÁö¸¸ ´ÙÇàÈ÷ óÀ½Äڵ带 »ç¿ëÇÏ¸é ¸ðµÎ Á¤»óÀûÀ¸·Î Àß ÀÛµ¿ ÇÏ¿´´Ù. AJAX ¸¦ »ç¿ëÇÏ¸é¼ °¡Àå Å« °ñÄ¡°Å¸®°¡ ¹Ù·Î ÀÌ·± ÄÚµåÀÌ´Ù. ÇϷ绡¸® W3C Ç¥ÁØÀ» ±¸ÇöÇϵµ·Ï ºê¶ó¿ìÀú Àü¿µ¿¡µµ °¢¼ºÀÌ Ã˱¸µÈ´Ù.
|