php에서 폼데이터를 전송하는 방식은 POST방식과 GET방식이 있습니다.
일반적으로 폼을 통한 데이터전송은 POST방식을 사용합니다.
form1.php파일은 폼에 내용을 작성하고 데이터 전송하는 파일이고
form_action1.php파일은 전송 받은 데이터를 처리하는 파일입니다.
아래는 form1.php파일의 코드 설명입니다.
There are two ways to transmit form data in PHP: POST method and GET method.
Generally, data transmission through forms uses the POST method.
The form1.php file is a file that writes content in the form and transmits data.
The form_action1.php file is a file that processes transmitted data.
Below is the code explanation of the form1.php file.
1.파일존재 여부 체크(Check whether file exists)
– is_file(“FILE NAME”) : 파일이 존재하면 true,존재하지 않으면 false반환
Returns true if the file exists, false if it does not exist.
– $form_action_name : 이 변수에 데이터 전송하기 위한 위치 파일 이름을 저장한다.
form_action1.php파일이 존재하지 않으면 form1.php파일이름을 저장한다.
Store the location file name for data transfer in this variable.
If the form_action1.php file does not exist, save the form1.php file name.
<?php
$fileName = "./form_action1.php";
if(!is_file($fileName)){
$form_action_name = "./form1.php";
}else{
$form_action_name = "./form_action1.php";
}
?>
2.부트스트랩 CDN방식 링크(Bootstrap CDN link)
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
3.뷰포트(View Port)
– 휴대폰으로 볼 때 화면 해상도를 최적화 합니다.
– Optimizing screen resolution when viewing on a phone.
<meta name="viewport" content="width=device-width, initial-scale=1.0">
4.데이터 전송될 파일 이름 출력(Output file name to which data will be transferred)
<form action="<?php echo $form_action_name; ?>" method="post">
5.데이터 입력(Data input)
– 아래에서 name =”텍스트 필드 이름” 이 부분의 “텍스트 필드 이름” 이 부분이 변수명이 된다.
– 이 변수명으로 form_action1.php파일에서 데이터를 처리하게 된다.
– Below, the “text field name” part of ‘ name = “text field name” ‘ becomes the variable name.
– Data is processed in the form_action1.php file with this variable name.
<input type="text" class="form-control" id="name" placeholder="당신의 이름을 입력하세요(Enter Your Name)" name="name">
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd">
6.전체코드(full code)
<?php
$fileName = "./form_action1.php";
if(!is_file($fileName)){
$form_action_name = "./form1.php";
}else{
$form_action_name = "./form_action1.php";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 5 Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<form action="<?php echo $form_action_name; ?>" method="post">
<div class="mb-3 mt-3">
<label for="name" class="form-label">이름(Name):</label>
<input type="name" class="form-control" id="name" placeholder="당신의 이름을 입력하세요(Enter Your Name)" name="name">
</div>
<div class="mb-3">
<label for="pwd" class="form-label">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pswd">
</div>
<div class="form-check mb-3">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="remember"> Remember me
</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>
7.실행확인(Check execution)
https://freelifemakers.org/lec/form1.php