Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính action của Form trong HTML

Thuộc tính action của thẻ <form> chỉ định URL nhận dữ liệu khi form được submit (gửi đi).

Cú pháp

<form action="URL"></form>

Giá trị

  • URL tuyệt đối: https://example.com/submit
  • URL tương đối: /submit, submit.php
  • Rỗng action="" hoặc thiếu: submit về chính trang hiện tại
  • mailto:email@example.com: mở client email (hiếm dùng)

Ví dụ

<!DOCTYPE html>
<html>
<style>
  body { color: #000; height: 100vh; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat; text-align: center; }
  input { width: 315px; display: block; margin: 1rem auto; border: 2px solid #fff; padding: 8px; background: transparent; border-radius: 20px; outline: none; }
  ::placeholder { color: #000; }
  .btn { background: #db133a; border: none; height: 2rem; border-radius: 20px; width: 330px; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem auto; }
</style>
<body>
<h1>Demo form action</h1>
<form action="/action_page.php" method="get">
  <input type="text" name="firstname" placeholder="Nhập Tên">
  <input type="text" name="lastname" placeholder="Nhập Họ">
  <input type="submit" value="GỬI" class="btn">
</form>
</body>
</html>

Kết quả

Điền form, nhấn "GỬI" → trình duyệt chuyển đến /action_page.php?firstname=...&lastname=... (vì method=get).

Lưu ý: Luôn validate server-side. action có thể bị override bằng formaction trên nút submit. Dùng method="post" cho dữ liệu nhạy cảm.