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

3
  • 3
    Js is client side script, while php is the server side script. The php script is executed on the server side, where js code has not been executed. Commented Nov 14, 2017 at 9:40
  • I believe the title is unclear. I think OP wants to reload the page with JS populating the uid query parameter and use PHP to display it on reload. Commented Nov 14, 2017 at 9:43
  • if (isset($_GET["uid"])) echo $_GET["uid"];. This will check for the existence of the variable before it displays it. It won't exist until you reload the page via your JS function. Therefore this code will stop it trying to display the value when you have first loaded the page manually. Commented Nov 14, 2017 at 10:31

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.