HTML enctype 속성이란?
HTML enctype 속성은 폼(form) 데이터를 서버로 전송할 때 적용되는 인코딩 형식을 지정합니다. 이 속성은 method="post" 방식으로 폼을 제출할 때 지정할 수 있으며, GET 방식에서는 사용되지 않습니다.
enctype 속성에는 다음과 같은 값을 설정할 수 있습니다.
| 값 | 설명 |
|---|---|
| application/x-www-form-urlencoded | 기본값입니다. 모든 문자가 전송되기 전에 인코딩됩니다(공백은 "+" 기호로 변환되고, 특수 문자는 ASCII HEX 값으로 변환됩니다). |
| multipart/form-data | 문자를 인코딩하지 않습니다. 파일 업로드 컨트롤이 포함된 폼을 사용할 경우 반드시 이 값을 지정해야 합니다. |
| text/plain | 공백은 "+" 기호로 변환되지만, 특수 문자는 인코딩되지 않습니다. |
이제 HTML enctype 속성의 실제 사용 예제를 살펴보겠습니다.
예제
<!DOCTYPE html>
<html>
<head>
<title>HTML enctype attribute</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
* {
padding: 2px;
margin:5px;
}
input[type="button"] {
border-radius: 10px;
}
</style>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post">
<fieldset>
<legend>HTML-enctype-attribute</legend>
<label for="EmailSelect">Email Id:
<input type="email" id="EmailSelect">
<input type="button" onclick="getUserEmail('david')" value="David">
<input type="button" onclick="getUserEmail('shasha')" value="Shasha"><br>
<input type="button" onclick="login()" value="Login">
</label>
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputEmail = document.getElementById("EmailSelect");
function getUserEmail(userName) {
if(userName === 'david')
inputEmail.value = 'davidMiller@MNC.com';
else
inputEmail.value = 'shashaGreen@MNC.com';
}
function login() {
if(inputEmail.value !== '')
divDisplay.textContent = 'Successful Login. Hello '+inputEmail.value.split("@")[0];
else
divDisplay.textContent = 'Enter Email Id';
}
</script>
</body>
</html>실행 결과
위 코드를 실행하면 다음과 같은 결과를 확인할 수 있습니다.
1) 이메일 필드가 비어 있는 상태에서 'Login' 버튼을 클릭한 경우 −

2) 이메일 필드에 값이 입력된 상태에서 'Login' 버튼을 클릭한 경우 −
