728x90
1. index.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>동적 검색 예제</title>
</head>
<body>
<input type="text" id="vl" name="vl"/>
<button onclick="submit()">전송</button>
<div id="response"></div>
<script>
const submit = () => {
const value = document.getElementById('vl').value;
fetch('test1.php', {
method: 'PUT', // POST 메소드를 사용
headers: {
'Content-Type': 'application/x-www-form-urlencoded', // 데이터 형식 지정
},
body: `vl=${encodeURIComponent(value)}` // POST 본문에 데이터 포함
})
.then(response => response.text()) // 서버로부터의 응답을 텍스트로 변환
.then(data => {
document.getElementById('response').innerText = data; // 결과를 div에 표시
console.log(data);
})
.catch(error => {
console.error('Error:', error);
document.getElementById('response').innerText = '오류가 발생했습니다.';
});
}
</script>
</body>
</html>
2.test.php >> PUT과 DELETE는 pasing해주어야 한다.
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$vl = $_POST['vl'];
echo $vl;
}else if($_SERVER['REQUEST_METHOD'] == 'GET') {
$vl = $_GET['vl'];
echo $vl;
}else if($_SERVER['REQUEST_METHOD'] == 'PUT') {
parse_str(file_get_contents("php://input"), $data); // PUT 데이터 읽기
$vl = $data['vl'];
echo $vl;
}else if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
// DELETE 요청 처리도 비슷하게 수정할 수 있습니다.
parse_str(file_get_contents("php://input"), $data); // DELETE 데이터 읽기
$vl = $data['vl'];
echo $vl;
}
?>
728x90
'JavaScript' 카테고리의 다른 글
바닐라 JS : XMLHttpRequest(ajax) - 모듈화 (0) | 2024.06.11 |
---|---|
js. content.indexOf()함수 (0) | 2023.12.12 |
JavaScript : PHP header()페이지 이동과 비슷한 JS 페이지 이동 location.href = "도메인"; (0) | 2023.04.13 |
JavaScript : <button> 클릭 시 페이지 이동 (0) | 2023.04.12 |
JavaScript : function back() 뒤 페이지로 넘어가기 (0) | 2023.04.11 |