개발자 (Developer)/백 엔드 (Back-End)
GET / POST 메소드로 웹 서버에 데이터 전달해보기
하늬아시
2023. 4. 5. 21:45
※저번 apache default main page 수정 방법에서 이어지는 내용입니다.
/var/www/html 경로에서 index.html을 아래와 같이 수정한 상태입니다.
<!doctype html>
<html lang="en">
<head>
<meta chatset="UTF-8">
<title>essentail ;</title>
</head>
<body>
<h1>GET LOGIN</h1>
<form action="./get_response.php" method="get">
<input type="text" name="name" placeholder="Username" id="name" required><br/>
<input type="text" name="userid" placeholder="ID" id="userid" required><br/>
<input type="text" name="email" placeholder="Email" id="email" required><br/>
<input type="password" name="password" placeholder="Password" id="password" pattern".{8,15} required title="8 to 15 characters"><br/>
<input type="submit" value="GET Login">
</form><br/><br/>
<h1>POST LOGIN</h1>
<form action="./post_response.php" method="post">
<input type="text" name="name" placeholder="Username" id="name" required><br/>
<input type="text" name="userid" placeholder="ID" id="userid" required><br/>
<input type="text" name="email" placeholder="Email" id="email" required><br/>
<input type="password" name="password" placeholder="Password" id="password" pattern".{8,15} required title="8 to 15 characters"><br/>
<input type="submit" value="POST Login">
</form>
</body>
</html>
과제의 내용이 GET / POST 메소드로 웹 서버에 데이터 전달하기였으니 잘 갔는지 확인하는 방법을 만들겠습니다.
/var/www/html 경로에 vim편집기로 get_response.php 파일 생성 후 아래와 같이 코딩하고 저장합니다.
<?php
$name = $_GET["name"];
$userid = $_GET["userid"];
$email = $_GET["email"];
$password = $_GET["password"];
?>
<html>
<head>
<title>essentail ;</title>
</head>
<body>
<h1>GET INFO</h1>
<?php
echo "이름은 ".$name."이고, ID는 ".$id."이고, Email은 ".$email."입니다.";
?>
</body>
</html>
그리고 정보를 입력 후에 GET Login 버튼을 누르면 아래의 사진과 같이 GET 메소드로 보낸 정보가 주소창에 있는 것을 확인하실 수 있습니다.
POST 메소드도 똑같습니다.
vim편집기로 post_response.php 파일 생성 후 아래와 같이 코딩하고 저장합니다. 마찬가지로 정보 입력 후에 POST Login 버튼을 누르면
<?php
$name = $_POST["name"];
$userid = $_POST["userid"];
$email = $_POST["email"];
$password = $_POST["password"];
?>
<html>
<head>
<title>essentail ;</title>
</head>
<body>
<h1>POST INFO</h1>
<?php
echo "이름은 ".$name."이고, ID는 ".$id."이고, Email은 ".$email."입니다.";
?>
</body>
</html>
아래와 같이 주소창에 정보가 없는 것을 볼 수가 있습니다.