Performance: Form submission basics
Form submission affects page load speed and interaction responsiveness when sending data to the server.
<form id="myForm"> <input type="text" name="name" required> <button type="submit">Send</button> </form> <script> document.getElementById('myForm').addEventListener('submit', async e => { e.preventDefault(); const formData = new FormData(e.target); await fetch('/submit', { method: 'POST', body: formData }); alert('Form sent without page reload!'); }); </script>
<form action="/submit" method="post"> <input type="text" name="name" required> <button type="submit">Send</button> </form>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Traditional form submit (full reload) | Destroys and rebuilds entire DOM | Multiple reflows due to full page layout | High paint cost for entire page | [X] Bad |
| JavaScript async submit (fetch API) | No DOM rebuild, only updates needed | No reflows triggered by submit itself | Minimal paint cost, only updates if needed | [OK] Good |
action attribute in an HTML form specify?action attributeaction attribute tells the browser where to send the form data when the user submits the form.method attribute controls how data is sent, not where. Styling and input types are unrelated to action.action = URL destination [OK]action and method attributes are needed. The method should be 'post' and action should be '/submit'. The order of attributes does not affect correctness.<form method='post' action='submit'> has incorrect action (missing leading '/'). The <form action='/submit' method='post'> uses correct attribute names and values. The one with method='get' uses wrong method for POST. The one with method='submit' uses invalid method and action values.<form action='/send' method='get'> <input type='text' name='username' value='alice'> <input type='submit'> </form>
<form action='/submit' method='post'> <input type='text' name='email'> <input type='submit' value='Send'> </form>
<label> element, which helps screen readers and improves usability.<form action='/feedback' method='post'>\n <label for='comments'>Comments</label>\n <textarea id='comments' name='comments'></textarea>\n <button type='submit'>Send Feedback</button>\n</form> and B have method='post', but B lacks label.<form action='/feedback' method='post'>\n <label for='comments'>Comments</label>\n <textarea id='comments' name='comments'></textarea>\n <button type='submit'>Send Feedback</button>\n</form> uses a <label> linked to the textarea with for and id, improving accessibility. The submit button is a <button> with clear text.<form action='/feedback' method='get'>\n <label>Comments</label>\n <textarea id='comments'></textarea>\n <button>Send Feedback</button>\n</form> uses GET method and missing name attribute on textarea, and label is not linked. <form action='/feedback'>\n <label for='comments'>Comments</label>\n <textarea id='comments' name='comments'></textarea>\n <input type='submit' value='Send Feedback'>\n</form> misses method attribute, so defaults to GET.Pass the test and this pattern moves unknown