user3214728
user3214728

Reputation: 13

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>

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"

Upvotes: 0

Views: 92

Answers (1)

Jim Wright
Jim Wright

Reputation: 6058

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>

Upvotes: 3

Related Questions