如何用PHP isset拦截JS表单提交及实现PHP触发表单提交
问题与解决方案
问题描述
我需要用PHP的isset方法拦截JavaScript提交的多步骤表单。表单通过JS的nextPrev函数控制步骤切换,最后调用form.submit()提交。尝试用if(isset($_POST["nextBtn"]) && $_POST["nextBtn"] == "submit")拦截但未成功,求解决办法,同时想了解PHP中如何触发表单提交。
相关代码
表单HTML代码
<form action="" id="regForm" name="NOME_FORM" method="post" role="search" action="<?php echo esc_url( home_url( '/grow-it' ) ); ?>"> <h1>Register:</h1> <!-- One "tab" for each step in the form: --> <?php foreach ( $grow as $risultato ) { $username = $risultato->username; $insta = "https://www.instagram.com/".$username; ?> <div class="tab"> <a target="_blank" href="<?php echo $insta; ?>" type="button" id="doStuffBtn" onclick="doStuff()"> Followe <?php echo $username; ?> per andare avanti </a> </div> <?php }?> </div> <div class="col-md-12"> <div style="overflow:auto;"> <div style="float:right;"> <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button> <button type="button" name="nextBtn" id="nextBtn" onclick="nextPrev(1)">Next</button> </div> </div> <!-- Circles which indicates the steps of the form: --> <div style="text-align:center;margin-top:40px;"> <?php foreach ( $grow as $risultato ) {?> <span class="step"></span> <?php } ?> </div> </div> </form>
JS代码
var currentTab = 0; // Current tab is set to be the first tab (0) showTab(currentTab); // Display the current tab var nextAvailable = false; function showTab(n) { var x = document.getElementsByClassName("tab"); x[n].style.display = "block"; if (n == 0) { document.getElementById("prevBtn").style.display = "none"; } else { document.getElementById("prevBtn").style.display = "inline"; } if (n == (x.length - 1)) { document.getElementById("nextBtn").innerHTML = "Submit"; } else { document.getElementById("nextBtn").innerHTML = "Next"; } fixStepIndicator(n) } function doStuff(n) { nextAvailable = true; } function nextPrev(n) { if (nextAvailable || n < 0) { nextAvailable = false; var x = document.getElementsByClassName("tab"); if (n == 1 && !validateForm()) return false; x[currentTab].style.display = "none"; currentTab = currentTab + n; if (currentTab >= x.length) { document.getElementById("regForm").submit(); return false; } showTab(currentTab); } } function validateForm() { var x, y, i, valid = true; x = document.getElementsByClassName("tab"); y = x[currentTab].getElementsByTagName("input"); for (i = 0; i < y.length; i++) { if (y[i].value == "") { y[i].className += " invalid"; valid = false; } } if (valid) { document.getElementsByClassName("step")[currentTab].className += " finish"; } return valid; } function fixStepIndicator(n) { var i, x = document.getElementsByClassName("step"); for (i = 0; i < x.length; i++) { x[i].className = x[i].className.replace(" active", ""); } x[n].className += " active"; }
拦截尝试的PHP代码
if(isset($_POST["nextBtn"]) && $_POST["nextBtn"] == "submit") { ?> <script type="text/javascript"> window.location.href = "www.google.com" </script> <?php }
解决方案
一、表单拦截失败的原因及修复
核心原因
nextBtn是type="button"的按钮,这类按钮不会被表单提交自动纳入POST数据集合,只有type="submit"的按钮或表单内的输入控件(input/textarea等)才会随表单提交传递值。你仅修改了按钮的显示文本为"Submit",并未改变按钮类型或添加可被提交的标识值。
修复方案
提供两种可靠实现方式:
方案1:添加隐藏输入字段作为提交标识
在表单内部添加一个隐藏的input,提交时设置其值,PHP通过判断该字段来拦截:
<form ...> <!-- 原有表单内容 --> <input type="hidden" name="form_submitted" id="form_submitted" value="0"> <!-- 按钮区域 --> </form>
修改JS提交逻辑,设置隐藏字段值:
if (currentTab >= x.length) { document.getElementById("form_submitted").value = "1"; document.getElementById("regForm").submit(); return false; }
PHP判断代码调整为:
if(isset($_POST["form_submitted"]) && $_POST["form_submitted"] == "1") { ?> <script type="text/javascript"> window.location.href = "www.google.com"; </script> <?php exit; // 必须添加exit,阻止后续代码执行 }
方案2:动态切换按钮为submit类型
修改JS的showTab函数,当进入最后一步时,将按钮类型改为submit并设置提交值:
function showTab(n) { var x = document.getElementsByClassName("tab"); x[n].style.display = "block"; if (n == 0) { document.getElementById("prevBtn").style.display = "none"; } else { document.getElementById("prevBtn").style.display = "inline"; } var nextBtn = document.getElementById("nextBtn"); if (n == (x.length - 1)) { nextBtn.innerHTML = "Submit"; nextBtn.type = "submit"; nextBtn.name = "nextBtn"; nextBtn.value = "submit"; nextBtn.onclick = null; // 移除原有点击事件,避免冲突 } else { nextBtn.innerHTML = "Next"; nextBtn.type = "button"; nextBtn.onclick = function(){ nextPrev(1); }; } fixStepIndicator(n) }
此时你的原有PHP判断即可生效,但需添加exit:
if(isset($_POST["nextBtn"]) && $_POST["nextBtn"] == "submit") { ?> <script type="text/javascript"> window.location.href = "www.google.com"; </script> <?php exit; }
二、PHP中触发表单提交的方式
PHP是服务器端语言,无法直接在服务器端触发浏览器的表单提交行为,但可以通过以下两种方式实现类似效果:
1. 输出JS代码让浏览器自动提交
在PHP中输出JavaScript,页面加载后自动提交指定表单:
<?php // 满足条件时触发自动提交 if($your_condition){ echo '<script>document.getElementById("regForm").submit();</script>'; exit; } ?>
2. 服务器端重定向并传递数据
若无需用户交互,可通过header函数重定向到表单处理页面,同时通过GET或SESSION传递数据:
<?php // 方式1:GET传递参数 header("Location: /grow-it?param1=value1¶m2=value2"); exit; // 方式2:SESSION存储数据(需先启动会话) session_start(); $_SESSION["form_data"] = $your_form_data; header("Location: /grow-it"); exit; ?>
在目标页面/grow-it中,可通过$_GET或$_SESSION读取数据并处理。
内容的提问来源于stack exchange,提问作者user21397959
相关产品推荐
相关产品推荐

