이전: https://velog.io/@azurp158/4주차-과제WEB2
이전 작업과 이어진다.
//check_address.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src = './check_address.js'></script>
</head>
<body>
<input type="text" name="address" id="address" placeholder = "예시) 서울특별시 테헤란로" oninput="handleInput();">
<input type="button" name = "find" id = "find" value="확인">
<table>
<thead>
<tr>
<td width="70">우편번호</th>
<td width="500">도로명</th>
</tr>
</thead>
<tbody name = 'result' id = 'result'>
</tbody>
</table>
</body>
</html>
//check_address.js
function handleInput() {
const address = document.getElementById('address').value;
const query = `SELECT * FROM ZIPCODE WHERE DORO LIKE '%${address}%' LIMIT 5`;
// Ajax 요청 보내기
const xhr = new XMLHttpRequest();
xhr.open('GET', '/get_address.php?query=' + encodeURIComponent(query));
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
const response = JSON.parse(xhr.responseText);
// 응답 데이터 처리하기
updateUI(response);
}
};
xhr.send();
}
function updateUI(response) {
const tableBodyElement = document.getElementById('result');
tableBodyElement.innerHTML = ''; // 테이블 본문 요소 초기화
for (const record of response) {
const rowElement = document.createElement('tr'); // 새로운 tr 요소 생성
const ZIPNO = document.createElement('td'); // 새로운 td 요소 생성
const DORO = document.createElement('td');
ZIPNO.textContent = record.ZIP_NO; // td 요소에 데이터 설정
DORO.textContent = recodr.SIDO + record.DORO;
rowElement.appendChild(ZIPNO); // td 요소를 tr 요소에 추가
rowElement.appendChild(DORO);
tableBodyElement.appendChild(rowElement); // tr 요소를 테이블 본문 요소에 추가
DORO.addEventListener('click', function(){
window.opener.Document.getElementById('Roadaddr').value = DORO;
window.opener.Document.getElementById('Decide_Roadaddr').value = DORO;
window.opener.Document.getElementById('Roadaddr').disabled = true;
window.close();
})
}
}
//get_address.php
<?php
$conn = new mysqli('localhost', 'conn', 'Testnote!%89','test');
$q = $_GET['query'];
$result = $conn->query($q);
$rows = array();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
$rows[] = $row;
}
header('Content-Type: application/json');
echo json_encode($rows);
?>