Jquery 在 php 文件中用 URL 参数替换表单值

Jquery inside a php file to replace a form value with a URL Parameter

所以我有一个 PHP 表单处理程序,其中有一个隐藏字段。我一直在 header 中使用它,但是,我没有在 [=32] 中使用它=] 文件。我访问 jquery 库,使用 $(document).ready(function () 并在其中包含我的变量,以及 getURLParameter("tid"),然后是 ($tbody).html $( "tbody").html($("tbody").html().replace(/{{transaction_id}}/g,replaceString));

我不断收到错误消息:未捕获的引用错误:未定义 getURLParameter

我可能在这里遗漏了一些东西,因为我以前没有在 PHP 文件中这样做过。

我应该使用 .php 而不是 .html 吗?

这是代码片段:

<?php } ?>
    <table width="250"><tbody>
        <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
$(document).ready(function () {
    var replaceString = "NO TID";
    var urlTerm = getURLParameter("tid");
    if (urlTerm.trim().length > 0 && urlTerm != "null") {
        replaceString = decodeURIComponent(urlTerm).replace(/\+/g,' ');
    }
    $("input.transaction_id").html($("input.transaction_id").html().replace(/\{\{transaction_id\}\}/g,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="{{transaction_id}}"></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>

谢谢!

您不需要将其替换为jQuery。如果它的 php 你可以获得 url 参数,比如

var urlTerm = "<?php echo $_GET["tid"] ?>";

但是,如果您仍然想使用 JavaScript 来考虑解决方案 here 因为您需要在代码中的某处定义函数 getURLParameter 或忘记导入它。