如何重新加载页面并使用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>
I have added JS and HTML on the same page. Wwhen I execute the code I am getting the value in PHP but it also displays
"Undefined index: uid"
function myfun()
{
var nm = 12;
window.location.href =“bkd.php?uid =”+ nm;
}
&lt; form name =“frm”method =“GET”&gt;
&lt; input type =“text”value =“ghf”onclick = “myfun();”&gt;&lt; / input&gt;
&lt; input type =“hidden”value =“”name =“txtval”id =“txtval”&gt;&lt; / input&gt;
&lt;?php
echo $ _GET [“uid”];
?&gt;
&lt; / form&gt;
code> pre>
我在同一页面上添加了JS和HTML。 当我执行代码时,我在PHP中获取值,但它也显示 p>
“Undefined index:uid” p>
blockquote>
DIV>
答
The first time you load the page $_GET['uid']
might not be set so you need to check it. You can use array_key_exists() for this.
I would also recommend putting the onclick
action on a button rather than the text field.
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>