[form] : <form>
-앵커처럼 다른 문서가 실행되지만 데이터를 전달할 수 있음, 전달하려면 이동하기위한 별도의 input요소 필요
-전달방식은 method="get" , method="post" 가 있음
-get방식 : URL주소 뒤에 파라미터를 붙여서 데이터 전달, 데이터가 노출되어 위험하고 글자수 제약을 받음, 이미지 보내기 불가
-get방식을 쓰는 이유는 속도가 빠르고 북마크처럼 저장되어 있는 것을 읽어올 때 많이 사용
-post방식 : 데이터를 URL주소 뒤에 붙이지 않고 전송, 길이제한이 없고 보안이 유지됨, 이미지 보내기 가능
-전달하려면 별도의 input요소들이 필요함
-php를 작성하여 호스팅서버에 업로드
<form action="입력을 처리하는 서버 스크립트의 주소" 데이터 전달방식="방식" (post방식일 때 인코딩타입 필수)>
[get방식]
-html
<form action="./getTest.php" method="get">
<input type="text" name="title">
<input type="text" name="msg">
<input type="submit">
</form>
-getTest.php
<?php
header('Content-Type:text/html; charset=utf-8');
//폼 요소를 통해 get방식으로 전달된 값 받기
//식별자가 똑같아야함
$title = $_GET['title'];
$message= $_GET['msg'];
echo "<h2>$title</h2>";
echo "<p>$message</P>";
?>
[post방식]
-html
<form action="./postTest" method="post" enctype="application/x-www-form-urlencoded">
<input type="text" name="id" placeholder="아이디를 입력하세요">
<input type="password" name="pw" placeholder="비밀번호를 입력하세요">
<p>
gender :
<label><input type="radio" name="rg" value="male" checked>male</label>
<label><input type="radio" name="rg" value="female">female</label>
</p>
<p>
fruits :
<label><input type="checkbox" name="fruits[]" value="apple" checked>Apple</label>
<label><input type="checkbox" name="fruits[]" value="banana">Banana</label>
<label><input type="checkbox" name="fruits[]" value="orange">Orange</label>
</p>
<p>
message :
<textarea name="msg"cols="40" rows="5"></textarea>
</p>
<div>
<select name="brand">
<optgroup label="자동차 브랜드">
<option value="현대">현대</option>
<option value="기아">기아</option>
<option value="쉐보레">쉐보레</option>
</optgroup>
</select>
</div>
<p>
<input type="submit" value="회원가입">
<input type="reset" value="취소">
</p>
</form>
-postTest.php
<?php
header('Content-Type:text/html; charset=utf-8');
$id=$_POST['id'];
$pw=$_POST['pw'];
$gender=$_POST['rg'];
$message=$_POST['msg'];
$brand=$_POST['brand'];
//textarea 안에 작성한 글 중에 줄바꿈문자는 \n 을 사용함
//그래서 \n을 <br>로 자동변환해주는 함수를 사용해야됨
//메시지에 있는 \n을 br로 변환하고 메시지에 다시 넣기
$message = nl2br($message);
echo "$id<br>";
echo "$pw<br>";
echo "$gender<br>";
echo "$message<br>";
echo "$brand<br>";
//멀티초이스에 의해 선택된 값들은 배열로 전달받기에 반복문으로 값들 echo시킴
$fruits=$_POST['fruits'];
//$fruits는 배열이라서 배열의 개수만큼 보여지게 반복문 사용
//php언어에서 배열의 개수를 알 수 있는 함수 : count
$num=count($fruits);
for($i=0; $i>$num; $i++){
//php 문장 결합 연산자 : .
echo $fruits[$i] . ",";
}
?>
[파일 전송]
-파일전송은 post방식으로만 가능
-파일전송 시 form의 인코딩 타입은 멀티파티로 해야함
-파일전송 시 실제데이터는 서버에 있는 html안 임시(temp)저장소에 넣어두고 php에는 메타데이터(파일의 정보)만 전달됨
-php에 전달되는 데이터(배열로 전달) : 이름, 확장자(jpg), 크기, 저장장소, 에러유무 (원본파일명, 타입, 사이즈, 임시저장소, error정보)
-php전달 데이터명(정해진 이름***) : name, type, size, tmp_name, error
-html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>form test file</title>
</head>
<body>
<fieldset>
<legend>파일전송</legend>
<!-- 파일전송 할때는 반드시 인코딩타입을 지정해야함 enctype="multipart/form-data 로 -->
<form action="./file.php" method="post" enctype="multipart/form-data">
<input type="file" name="aaa" accept="image/png,image/jpeg"><br>
<input type="submit">
</form>
</fieldset>
<fieldset>
<legend>여러개의 파일전송</legend>
<form action="./file2.php" method="post" enctype="multipart/form-data">
<input type="file" name="bbb[]" multiple="multiple">
<!-- 완료버튼을 이미지로대체하기 -->
<input type="image" src="./image/seolak.jpg" width="100">
</form>
</fieldset>
</body>
</html>
-파일전송 file.php
<?php
header("Content-Type:text/html; charset=utf-8");
//폼으로 전달된 파일은 조금 특별한 방식으로 전달됨
//실제 파일데이터를 별도의 임시공간에 저장됨
//이 php로 전달되는 것을 파일의 데이터가 아니라 파일을 설명하는 데이터가 옴
//전달된 파일의 정보는 배열로 전달되는데 5개의 정해진 이름의 정보가 있음
$file=$_FILES['aaa'];
//$file은 배열 [5칸짜리]
$srcName=$file['name'];
$type=$file['type'];
$size=$file['size'];
$tmpName=$file['tmp_name'];
$error=$file['error'];
echo "$srcName<br>";
echo "$type<br>";
echo "$size<br>";
echo "$tmpName<br>";
echo "$error<br>";
//임시저장소에 있는 파일은 이 php가 끝나면 자동으로 사라짐
//그래서 영구적으로 저장하기 위해서는 위치를 이동해야함
//중복되는 파일이름이 있을 수 있기 때문에 파일 뒤에 날짜를 붙이고 이름하고 확장자도 같이 붙임
$dstName="./".date(YmdHis).$srcName;
move_uploaded_file($tmpName, $dstName);
?>
-여러개의 파일전송 file2.php
<?php
$files=$_FILES['bbb'];
//$files는 배열임 근데 파일이 여러개
echo count($files); //파일개수와 상관없이 무조건 5개가 나옴
//선택된파일의 개수를 알고 싶다면 (만약 3개의 파일을 받아 왔다면 이름칸에 3개의 파일이 있음)
$num=count($files['name']);
for($i=0; $i<$num; $i++){
$srcName=$files['name'][$i];
$size=$files['size'][$i];
$tmpName=$files['tmp_name'][$i];
echo "$srcName<br>";
echo "$size<br>";
echo "$tmpName<br>";
echo "===================<br>";
$dstName="./image/". date("YmdHis"). $srcName;
move_uploaded_file($tmpName,$dstName);
echo "<img src='$dstName'>";
echo "<br>===================<br>";
}
?>
'Web > Web' 카테고리의 다른 글
CSS 속성 - 폰트, 텍스트 스타일 (0) | 2022.05.19 |
---|---|
CSS 기능 (1) - 선택자 (0) | 2022.05.19 |
HTML5 (2) (0) | 2022.05.11 |
HTML5 (1) (0) | 2022.05.10 |
Web기초 (0) | 2022.05.10 |