Thuộc tính formEnctype của Input Submit trong HTML DOM dùng để thiết lập hoặc trả về giá trị thuộc tính formenctype của nút submit. Giới thiệu trong HTML5 cho input type submit. Dùng để chỉ định cách mã hóa dữ liệu form khi submit lên server.
Thuộc tính này chỉ hoạt động khi có method="post". Giá trị formenctype ghi đè enctype của phần tử <form>.
Cú pháp
Cú pháp để thiết lập thuộc tính formEnctype:
submitObject.enctype = encoding
Các giá trị encoding:
"application/x-www-form-urlencoded": Mã hóa tất cả ký tự (mặc định)."multipart/form-data": Không mã hóa, dùng cho upload file."text/plain": Chuyển space thành "+", không an toàn, không nên dùng.
Ví dụ
Ví dụ minh họa cho thuộc tính formEnctype:
<!DOCTYPE html> <html> <head> <script>
function changeEnc() {
document.getElementById("SUBMIT1").formEnctype = "application/x-www-form-urlencoded";
document.getElementById("Sample").innerHTML = "Thuộc tính formenctype bây giờ là 'application/x-www-form-urlencoded'";
}
</script> </head> <body>
<h1>Ví dụ thuộc tính formEnctype của Submit</h1>
<form id="FORM_1" action="/Sample.php" style="border:solid 2px green;padding:2px">
Tên người dùng: <input type="text" id="USR"> <br>
Vị trí: <input type="text" id="Loc"> <br><br>
<input type="submit" id="SUBMIT1" formenctype="multipart/form-data">
</form>
<br>
<button onclick="changeEnc()">THAY ĐỔI</button>
<p id="Sample"></p>
</body> </html>
Kết quả
Khi nhấn nút "THAY ĐỔI", formenctype chuyển từ "multipart/form-data" sang "application/x-www-form-urlencoded".