![](/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.