คุณสมบัติ formEnctype ของ HTML DOM Submit ใช้สำหรับตั้งค่าหรือส่งคืนค่าแอตทริบิวต์ formEnctype ของปุ่ม submit ถูกนำมาใช้ใน HTML5 สำหรับองค์ประกอบ input ที่มี type submit ใช้ระบุว่าข้อมูลฟอร์มควรเข้ารหัสอย่างไรเมื่อส่งไปยังเซิร์ฟเวอร์
คุณสมบัตินี้ทำงานก็ต่อเมื่อมีคุณสมบัติ method=”post” ค่าแอตทริบิวต์ formEnctype จะลบล้างค่าแอตทริบิวต์ enctype ที่เกี่ยวข้องกับองค์ประกอบ <form>
ไวยากรณ์
ไวยากรณ์สำหรับการตั้งค่าคุณสมบัติ formEnctype ของ submit:
submitObject.enctype = encoding
ที่นี่ encoding สามารถเป็น “application/x-www-form-urlencoded” หมายถึงเข้ารหัสอักขระทั้งหมดก่อนส่งและนี่คือการเข้ารหัสเริ่มต้น อีกค่าหนึ่งคือ “multipart/form-data” ระบุว่าไม่ควรเข้ารหัสอักขระใดๆ และใช้สำหรับอัปโหลดไฟล์ไปยังเซิร์ฟเวอร์ การเข้ารหัสที่สามคือ “text/plain” แปลงช่องว่างเป็น “+” เท่านั้น ไม่ควรใช้ text/plain เพราะไม่ปลอดภัย
ตัวอย่าง
มาดูตัวอย่างสำหรับคุณสมบัติ formEnctype ของ submit:
<!DOCTYPE html> <html> <head> <script> function changeEnc() { document.getElementById("SUBMIT1").formEnctype = "application/x-www-formurlencoded"; document.getElementById("Sample").innerHTML = "The formenctype attribute value is now 'application/x-www-form-urlencoded' "; } </script> </head> <body> <h1>Submit formEnctype property example</h1> <form id="FORM_1" action="/Sample.php" style="border:solid 2px green;padding:2px"> UserName: <input type="text" id="USR"> <br> Location: <input type="text" id=“Loc”> <br><br> <input type="submit" id="SUBMIT1" formenctype="multipart/form-data"> </form> <br> <button onclick="changeEnc()">CHANGE</button> <p id="Sample"></p> </body> </html>
ผลลัพธ์
จะได้ผลลัพธ์ดังนี้:

เมื่อคลิกปุ่ม CHANGE:
