แอตทริบิวต์ HTML enctype ระบุรูปแบบการเข้ารหัสสำหรับข้อมูลแบบฟอร์ม เมื่อใช้วิธี post ก็สามารถระบุแอตทริบิวต์ enctype ได้
ที่นี่แอตทริบิวต์ enctype สามารถมีค่าดังต่อไปนี้ -
| ค่า | คำอธิบาย |
|---|---|
| application/x-www-form-urlencoded | เป็นค่าเริ่มต้น อักขระทั้งหมดจะถูกเข้ารหัสก่อนที่จะส่ง (ช่องว่างจะถูกแปลงเป็นสัญลักษณ์ "+" และอักขระพิเศษจะถูกแปลงเป็นค่า ASCII HEX) |
| multipart/form-data | ไม่เข้ารหัสอักขระ ค่านี้จำเป็นหากผู้ใช้ใช้แบบฟอร์มที่มีตัวควบคุมการอัปโหลดไฟล์ |
| ข้อความ/ธรรมดา | ด้วยค่าของช่องว่าง enctype นี้จะถูกแปลงเป็นสัญลักษณ์ "+" แต่ไม่มีการเข้ารหัสอักขระพิเศษ |
ให้เรามาดูตัวอย่าง 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) คลิก 'เข้าสู่ระบบ' ปุ่มพร้อมช่องอีเมลว่าง -

2) หลังจากคลิก 'เข้าสู่ระบบ' ปุ่มพร้อมชุดฟิลด์อีเมล -
