HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML action Attribute
Photo Credit to CodeToFun
🙋 Introduction
In HTML, the action
attribute plays a crucial role in defining the URL to which a form should be submitted.
It is used within the <form> element to specify the server-side script or endpoint that will process the data submitted by the user.
🎯 Purpose of action
The primary purpose of the action
attribute is to provide the browser with the destination URL where the form data should be sent for processing.
This URL could point to a server-side script, a web service, or any other resource capable of handling form submissions.
💎 Values
The action
attribute accepts various values, typically represented as URLs. These values can be absolute or relative URLs, allowing for flexibility in specifying where the form data should be sent. Here are some examples:
- Relative URL: /submit_form - The form data is submitted to the "/submit_form" endpoint on the same domain.
- Absolute URL: https://example.com/process_form - The form data is sent to the specified absolute URL, allowing cross-domain form submissions.
- Empty Value: action="" - In some cases, you might want to submit the form to the same URL it originated from.
📄 Example
The action
attribute is added to the opening <form> tag and takes the form of a URL. Here's a basic example:
<form action="/submit_form" method="post">
<!-- Form elements go here -->
<input type="submit" value="Submit">
</form>
🧠 How it Works
In this example, the form data will be submitted to the /submit_form URL using the HTTP POST method.
🔄 Dynamic Values with JavaScript
You can dynamically set the action
attribute using JavaScript to modify the form submission behavior based on user interactions or other conditions. Here's a simple illustration:
<script>
// Dynamically set the action attribute for a form
document.getElementById("dynamicForm").action = "/dynamic_submit";
</script>
🧠 How it Works
In this script, the action
attribute of a form with the id "dynamicForm" is set to /dynamic_submit. This flexibility can be particularly useful in scenarios where the form submission endpoint needs to change dynamically.
🏆 Best Practices
- Ensure that the URL specified in the
action
attribute is a valid and accessible endpoint capable of handling form submissions. - When dealing with sensitive data, use secure, HTTPS URLs in the
action
attribute to protect the data during transmission. - Be aware that some security considerations may arise when dynamically setting the
action
attribute, so thoroughly validate and sanitize user inputs if they are used to construct the URL dynamically.
🎉 Conclusion
The action
attribute is a fundamental aspect of HTML forms, guiding the browser on where to send user-entered data. Understanding how to use and customize this attribute is crucial for creating effective and functional web forms.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML action Attribute), please comment here. I will help you immediately.