function myfun()
{
var nm=12;
window.location.href="bkd.php?uid="+nm;
}
<form name="frm" method="GET">
<input type="text" value="ghf" onclick="myfun();"></input>
<input type="hidden" value="" name="txtval" id="txtval"></input>
<?php
echo $_GET["uid"];
?>
</form>
我在同一页面添加了 JS 和 HTML。当我执行代码时,我得到了 PHP 中的值,但它也显示了
"Undefined index: uid"
最佳答案
第一次加载页面时,$_GET['uid']
可能未设置,因此您需要检查它。您可以使用array_key_exists()为此。
我还建议将 onclick
操作放在按钮上而不是文本字段上。
function myfun()
{
var nm=12;
window.location.href="bkd.php?uid="+nm;
}
<form name="frm" method="GET">
<input type="text" value="ghf"></input>
<input type="hidden" value="" name="txtval" id="txtval"></input>
<input type="button" onclick="myfun();"></input>
<?php
if (array_key_exists("uid", $_GET)) {
echo $_GET["uid"];
}
?>
</form>
关于javascript - 如何使用 PHP + JS 重新加载页面并显示查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47282101/