HTML 是靜態標記語言,**無法**直接寫入資料庫、管理 Session、驗證身分。實務架構:**前端純 HTML + 後端 API (ASP.NET Core, Node.js, PHP, Java...)**,透過 AJAX/Fetch 交換 JSON 資料。
<form id="surveyForm" action="/api/survey" method="POST">
<input type="hidden" name="csrf_token" value="...">
<label>Q1: <input type="text" name="q1" required></label>
<label>Q2: <select name="q2"><option value="A">A</option><option value="B">B</option></select></label>
<button type="submit">送出</button>
</form>
<script>
document.getElementById('surveyForm').addEventListener('submit', async e => {
e.preventDefault();
const fd = new FormData(e.target);
const res = await fetch(e.target.action, { method: 'POST', body: fd, credentials: 'include' });
if (res.ok) alert('提交成功'); else alert('失敗:' + await res.text());
});
</script>
靜態頁面間傳遞 ID:
`survey.html?user=123&token=abc`
前端解析:
const params = new URLSearchParams(location.search);
const userId = params.get('user'); // "123"
const token = params.get('token'); // "abc"
// 依參數 fetch 對應資料並填充表單
fetch(`/api/survey/${userId}?token=${token}`)
.then(r => r.json())
.then(data => populateForm(data));