![](/img/trans.png)
[英]How do i pass a javascript variable in querystring from one html page to other
[英]How to pass querystring values from php to an HTML page?
我知道有几个问题涉及如何将查询字符串传递到 HTML 页面,但我一直无法找到解决我的问题的问题。
我们有一个名为 viewrecs.php 的 php 页面。 此页面查询数据库并返回包括 recordId 在内的几条记录。 这个 id 的值被传递到一个名为 edit.html 的 HTML 页面,如下所示:
<a href="edit.html?recid='.$row['recordId'].'">Edit</a>
我正在尝试获取此 recordId 的值并将其存储为 edit.html 页面中的隐藏字段。
我尝试修改我在此论坛上找到的以下 JavaScript 来解决此问题,但 recordId 的值返回空白。
<script type="text/javascript">
$(document).ready(function () {
var x = getParameterByName("recid"); ///get url parameter value
alert("recid : " + x);
function getParameterByName(id) {
id = id.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + id + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
});
</script>
然后是 HTML:
<input type="hidden" name="recid"/>
如何从 php 页面检索查询字符串的值并将其传递给 HTML 页面并将其存储在隐藏的表单字段中?
信用:我上面修改的 Javascript 最初属于 Mahmoude Elghandour
在您的 edit.html 页面中,您可以使用 inside head 标签
onload = function () {
const urlParams = new URLSearchParams(window.location.search);
const recordId = urlParams.get('recid');
document.getElementById("recId").value = recordId;
}
然后,在您的 html 中,您可以编写:
<input type="hidden" id="recId" name="recid">
希望您不介意在 HTML 元素中添加 ID。
var request_url=document.getElementById("recordId").href; var request_url=(request_url).match(/[recid][=]([0-9]+)[&]*/i); console.log(request_url[1]); document.getElementById("recid").value = request_url[1];
<!--<a id="recordId" href="edit.html?recid='.$row['recordId'].'">Edit</a>--> <a id="recordId" href="edit.html?recid=8888">Edit</a> <input type="hidden" name="recid" id="recid"/>
你也可以使用 javascript 函数
var url_string =document.getElementById("recordId").href; var url = new URL(url_string); var c = url.searchParams.get("recid"); document.getElementById("recid").value = c; //Or jquery $('#recid').val(c); console.log(c);
<!--<a id="recordId" href="edit.html?recid='.$row['recordId'].'">Edit</a>--> <a id="recordId" href="edit.html?recid=88588&somethingelse=123">Edit</a> <input type="hidden" name="recid" id="recid"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.