Is this how to put parameter?

Hi, can you help me please is this correct to put parameter in window.location ?,because my code did not work.


 <?php

    if(isset($_post['idno'])){
       $id = $_post['idno'];
      $age = $_post['age'];
  }

?>

<script type="text/javascript">
    var idno = '<?php echo $id;?>';
    var myage =  '<?php echo $age;?>';


        window.location.href=('myinformation.php?id='+idno+'&age='+myage);

</script>

Thank you in advance.

Your current code won’t work because the PHP tags are within quotes which the PHP compiler can’t see, however the browser will see this as a string. See the below which is one way to output PHP variables into JavaScript code however a much more common method would be to use a JSON array.

Plain JavaScript

var idno  = <?php echo "'$id'"; ?>,
    myage = <?php echo "'$age'"; ?>;

window.location.href = 'myinformation.php?id=' + idno + '&age=' + myage;

Using a JSON array (recommended)

<?php
	
$data = array(
	'idno' => (isset($_POST['idno']) && $_POST['idno']) || '',
	'age'  => (isset($_POST['age']) && $_POST['age'])   || ''
);

?> 
<script type="text/javascript"> 
	var data = <?php json_encode($data); ?>;
	window.location.href = 'myinformation.php?id=' + data.idno + '&age=' + data.myage;
</script>

Hi chris.upjohn, wow thank you for the solution it’s working now…and also i learn that it is okay to put isset in array and pass to json.

Thank you so much.