html 파일에서 php 파일을 불러오는 방법은 어떻게 될까요?
본문
html 파일에서 php 파일을 불러오려고 합니다.
php 파일은 안에 select 값을 해서 값을 불러오고 있습니다.
쉽게 설명하면 그누보드가 아닌 php 프로그램인데 최신글을 10개 까지 가져오려고 합니다.
그런데 html에서 php 파일 불러오는 방법이 어떻게 될까요?
<? include "/notice.phpl" ?>
<? require_once './notice.php'; ?>
이렇게 해도 안되서 ajax로 불러와야 하나 알아보고 있습니다.
html에서 php 값 불러오는 방법이 있을까요?
답변 1
순수 html 파일에서는 php 코드가 인식되지 않습니다.
include, require_once 는 php 명령어 입니다.
ajax 로 처리하는 방법 예제입니다.
<div id="noticeContainer"
~
document.getElementById('noticeContainer').innerHTML = data;
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>최신 공지사항</title>
</head>
<body>
<h1>최신 공지사항</h1>
<!-- 공지사항 데이터가 로드될 컨테이너 -->
<div id="noticeContainer" class="notice-list">
</div>
<script>
// 페이지 로드 시 공지사항 데이터 가져오기
document.addEventListener('DOMContentLoaded', function() {
fetch('notice.php')
.then(response => {
if (!response.ok) {
throw new Error('네트워크 응답이 올바르지 않습니다');
}
return response.text();
})
.then(data => {
document.getElementById('noticeContainer').innerHTML = data;
})
.catch(error => {
console.error('데이터를 가져오는 중 오류가 발생했습니다:', error);
document.getElementById('noticeContainer').innerHTML =
'<div class="error">데이터를 불러오는 데 실패했습니다. 나중에 다시 시도해 주세요.</div>';
});
});
</script>
</body>
</html>
답변을 작성하시기 전에 로그인 해주세요.