PHP文件内使用jQuery替换表单值为URL参数时getURLParameter未定义报错
错误原因
你遇到的getURLParameter is not defined报错和文件后缀是.php还是.html无关,问题核心是:getURLParameter不是JavaScript或jQuery的内置方法,是需要自行实现的自定义函数,你没有定义该函数就直接调用,因此触发引用错误。
修复方案
你可以选择前端JS实现或者后端PHP实现两种方式:
方案1:前端JS修复(无需修改后端逻辑)
- 先补全
getURLParameter函数的定义,放在调用代码之前 - 修正隐藏输入框的赋值逻辑:input元素没有内部HTML,不要用
html()方法,直接用jQuery的val()方法赋值即可,不需要做字符串替换,效率更高也避免DOM污染。
修正后的完整代码如下:
<?php } ?> <table width="250"><tbody> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script> // 先定义getURLParameter函数 function getURLParameter(name) { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); var results = regex.exec(location.search); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); } $(document).ready(function () { var replaceString = "NO TID"; var urlTerm = getURLParameter("tid"); if (urlTerm.trim().length > 0 && urlTerm != "null") { replaceString = urlTerm; // 函数内部已经做了解码和+替换,不需要重复处理 } // 直接给隐藏input赋值,不需要替换HTML $("input.transaction_id").val(replaceString); }); </script> <tr><td><form id="signupform" class="signup_form" action="/testing-kirby.php" method="post"><div><div><input class="signup-name-area" style="height: 32px; width: 325px;" name="name" type="text" placeholder="Full Name"><br><br><input class="signup-email-area" style="height: 32px; width: 325px;" name="email" required="required" type="email" placeholder="email@example.com"><div style=" margin-top: 15px;"><center><input class="signup-button-2" style="background-color: red; height: 48px;" type="submit" value="Sign Petition"></center></div><input name="loc" type="hidden" value="header"></div></div><input name="tid" class="transaction_id" type="hidden" value=""></div></form><p style="text-align: center;">Sponsored by:</p><p style="text-align:center;"><img src="https://wolfdaily.com/wp-content/uploads/2020/05/Wolf-Daily-Final-160h.png" width="200"></p></td></tr></tbody></table>
方案2:PHP直接实现(更简单,不需要依赖JS)
如果你的文件是.php后缀,可以直接在后端输出参数,不需要前端处理:
<?php $tid = isset($_GET['tid']) ? trim($_GET['tid']) : 'NO TID'; // 防止XSS注入 $tid = htmlspecialchars($tid, ENT_QUOTES); ?> <table width="250"><tbody> <tr><td><form id="signupform" class="signup_form" action="/testing-kirby.php" method="post"><div><div><input class="signup-name-area" style="height: 32px; width: 325px;" name="name" type="text" placeholder="Full Name"><br><br><input class="signup-email-area" style="height: 32px; width: 325px;" name="email" required="required" type="email" placeholder="email@example.com"><div style=" margin-top: 15px;"><center><input class="signup-button-2" style="background-color: red; height: 48px;" type="submit" value="Sign Petition"></center></div><input name="loc" type="hidden" value="header"></div></div><input name="tid" class="transaction_id" type="hidden" value="<?php echo $tid; ?>"></div></form><p style="text-align: center;">Sponsored by:</p><p style="text-align:center;"><img src="https://wolfdaily.com/wp-content/uploads/2020/05/Wolf-Daily-Final-160h.png" width="200"></p></td></tr></tbody></table>
注:原代码中
<div style=" margin-top: 15px;"缺少闭合标签>,上述代码中已同步修复。
内容的提问来源于stack exchange,提问作者KirbyBell561
相关产品推荐
相关产品推荐

