如何将querystring值从php传递到HTML页面



我知道有几个问题涉及如何将querystring传递到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页面检索querystring的值并将其传递到HTML页面,并将其存储在隐藏的表单字段中?

信用:我在上面修改的Javascript最初属于Mahmoude Elghandour

在您的edit.html页面中,您可以使用内部头标记

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"/>

最新更新