|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Pizza Order Form</title> |
| 6 | +</head> |
| 7 | +<body id="page-body"> |
| 8 | + <form id="pizza-form" method="post"> |
| 9 | + <label for="custname">Customer name:</label> |
| 10 | + <input type="text" id="custname" name="custname"><br> |
| 11 | + |
| 12 | + <label for="custtel">Telephone:</label> |
| 13 | + <input type="tel" id="custtel" name="custtel"><br> |
| 14 | + |
| 15 | + <label for="custemail">E-mail address:</label> |
| 16 | + <input type="email" id="custemail" name="custemail"><br> |
| 17 | + |
| 18 | + <fieldset> |
| 19 | + <legend>Pizza Size</legend> |
| 20 | + <label><input type="radio" name="size" value="small"> Small</label> |
| 21 | + <label><input type="radio" name="size" value="medium"> Medium</label> |
| 22 | + <label><input type="radio" name="size" value="large"> Large</label> |
| 23 | + </fieldset> |
| 24 | + |
| 25 | + <fieldset> |
| 26 | + <legend>Pizza Toppings</legend> |
| 27 | + <label><input type="checkbox" name="topping" value="bacon"> Bacon</label> |
| 28 | + <label><input type="checkbox" name="topping" value="cheese"> Extra Cheese</label> |
| 29 | + <label><input type="checkbox" name="topping" value="onion"> Onion</label> |
| 30 | + <label><input type="checkbox" name="topping" value="mushroom"> Mushroom</label> |
| 31 | + </fieldset> |
| 32 | + |
| 33 | + <label for="comments">Delivery instructions:</label> |
| 34 | + <textarea id="comments" name="comments"></textarea><br> |
| 35 | + |
| 36 | + <button type="submit">Submit order</button> |
| 37 | + </form> |
| 38 | + |
| 39 | + <script> |
| 40 | + document.getElementById('pizza-form').addEventListener('submit', function (e) { |
| 41 | + e.preventDefault(); |
| 42 | + var form = e.target; |
| 43 | + var data = { form: {} }; |
| 44 | + |
| 45 | + // Collect all named fields |
| 46 | + var fd = new FormData(form); |
| 47 | + fd.forEach(function (value, key) { |
| 48 | + if (key in data.form) { |
| 49 | + // Multiple values for same key -> promote to array |
| 50 | + if (!Array.isArray(data.form[key])) { |
| 51 | + data.form[key] = [data.form[key]]; |
| 52 | + } |
| 53 | + data.form[key].push(value); |
| 54 | + } else { |
| 55 | + data.form[key] = value; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + document.body.textContent = JSON.stringify(data); |
| 60 | + }); |
| 61 | + </script> |
| 62 | +</body> |
| 63 | +</html> |
0 commit comments