01index.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>
<style>
a{text-decoration: none; color: green;}
</style>
</head>
<body>
<!-- view-source:localhost/php/file/01index.php -->
<!-- http://localhost/php/file/01index.php -->
<!-- ?php
var_dump(scandir('data/'));
?> -->
<h1>리빙페이지</h1>
<ul>
<!-- url 주소창의 내용을 사용해서 내용을 받음 -->
<!-- 미리 만들어놓은 text파일(living..)의 내용을 함게 전송시킴 -->
<!-- (1) -->
<!-- <li><a href="01index.php?title=햇빛 좋은 어느날, 우리집&text=living1">
햇빛 좋은 어느날, 우리집</a></li>
<li><a href="01index.php?title=북한산 산세를 병풍처럼 두른 한옥마을&text=living2">
북한산 산세를 병풍처럼 두른 한옥마을</a></li>
<li><a href="01index.php?title=오래된 주책 셀프 리모델링, 주방타일&text=living3">
오래된 주책 셀프 리모델링, 주방타일</a></li> -->
<!-- (2) -->
<?php
$lists = scandir('data/');
// var_dump($lists);
for ($i=0; $i<count($lists); $i++) {
$title = $lists[$i];
if($lists[$i] != "." && $lists[$i] != ".."){
// echo "<li><a href='01index.php?title=".$title."'>".$title."</a></li>";
// echo "<li><a href='01index.php?title=$title'>$title</a></li>";
echo "<li><a href='01index.php?title=${title}'>${title}</a></li>";
}
}
?>
</ul>
<h2>
<?php
// 05. 26
// isset ) 존재하는지
// 첫화면 ) 있을 때만 불러오기
if(isset($_GET['title'])) {
echo $_GET['title'];
} else {
echo "환영합니다.";
}
// echo $_GET['title'];
//주소창의 title의 내용이 화면에 출력됨
?>
</h2>
<p>
<?php
// 05. 26
// isset ) 존재하는지
// 첫화면 ) 있을 때만 불러오기
if(isset($_GET['title'])) {
echo file_get_contents("data/".$_GET['title']);
} else {
echo "반가워요.";
}
// echo file_get_contents("data/".$_GET['title']);
//주소창의 title의 내용과 미리 만들어놓은 text파일(living..)의 내용이 화면에 함께 출력됨
?>
</p>
<br>
<h3>
<a href="03write.php">🐣03write.php) 파일추가하기</a>
</h3>
</body>
</html>
02write_process.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>
</head>
<body>
<!-- http://localhost/php/file/02write_process.php -->
<?php
//file_put_contents(저장장소, 파일명, 내용);
$title = $_POST['title'];
$description = $_POST['desc'];
file_put_contents('data/'.$title, $description);
echo "(🐣03write_process, input 내용) 파일이 추가되었습니다.";
//주소창에 아래의 주소를 넣어준다.
//localhost/php/file/03write_process.php?title=추가할제목&desc=내용입니다.
// 💚 리다이렉션 ) 페이지를 추가하면서 index로 옮겨줌, 다른 주소로 넘김
header('Location:01index.php');
?>
</body>
</html>
03write.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>
</head>
<body>
<!-- http://localhost/php/file/03write.php -->
<!-- (1) onsubmit 함수 -->
<form action="02write_process.php" method="post" onsubmit="return formCheck()">
<!-- (2) required ) 내용을 입력해야 전송됨 -->
<input type="text" name="title" id="titleBox">
<input type="text" name="desc" id="descBox">
<button type="submit">전송</button>
<button type="reset">취소</button>
</form>
<script>
// (1) input에 내용이 있는지 없는지 체크
function formCheck() {
let titlebox = document.querySelector('#titleBox');
let descbox = document.querySelector('#descBox');
if(!titlebox.value || !descbox.value) { // 내용이 없을 경우
alert ('제목과 내용을 입력하시오.');
return false;
}
}
</script>
</body>
</html>
'PHP' 카테고리의 다른 글
[PHP] book1 (0) | 2023.04.05 |
---|---|
[PHP] blog (0) | 2023.04.05 |
[PHP] include (0) | 2023.04.05 |
[PHP] session, cookie (0) | 2023.04.05 |
[PHP] mySQL 연결, 쿼리문 (0) | 2023.04.05 |