自定義表單前台提交提示「數據校驗不對,程序返回」,導致無法入庫。

表單隱藏字段 dede_fieldshash 為防偽造令牌,其 value 由 templets/plus/post_diyform.php 動態生成(根據表單字段配置計算哈希)。若前台模板為靜態複製的 HTML,value 為舊值,提交時與服務端重新計算的哈希不匹配,驗證失敗。
訪問系統自帶表單頁:
https://yourdomain.com/plus/diy.php?action=post&diyid=1
查看頁面源碼,複製 <form> 完整代碼(含最新 dede_fieldshash)。
方案 A:AJAX 代理提交(推薦)
<form id="myDiyForm" action="/plus/diy.php" method="post">
<input type="hidden" name="action" value="post"/>
<input type="hidden" name="diyid" value="1"/>
<!-- 動態注入 hash -->
<input type="hidden" name="dede_fieldshash" id="dede_fieldshash"/>
<!-- 您的自定義字段 -->
<input name="name" placeholder="姓名"/>
<input name="phone" placeholder="電話"/>
<button type="submit">提交預約</button>
</form>
<script>
// 頁面載入時請求官方表單頁獲取最新 hash
fetch('/plus/diy.php?action=post&diyid=1')
.then(r => r.text())
.then(html => {
const m = html.match(/name="dede_fieldshash" value="([^"]+)"/);
if (m) document.getElementById('dede_fieldshash').value = m[1];
});
// 提交攔截
document.getElementById('myDiyForm').addEventListener('submit', async (e) => {
e.preventDefault();
const fd = new FormData(e.target);
const res = await fetch('/plus/diy.php', {method:'POST', body:fd});
const txt = await res.text();
alert(txt.includes('成功') ? '提交成功' : '提交失敗:'+txt);
});
</script>
方案 B:iframe 嵌入官方表單(最省事)
<iframe src="/plus/diy.php?action=post&diyid=1" width="100%" height="500" frameborder="0" scrolling="no"></iframe>
永遠不要靜態複製含
dede_fieldshash的表單 HTML。 必須在用戶打開頁面的瞬間,向服務端請求最新 hash,或直接使用系統自帶表單頁面。