[英]How can I reload a page and display a query parameter with PHP + JS?
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中获取值,但也会显示
“未定义的索引: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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.