-1

I am trying to redirect to same page i am on but with some variable. The code i have written here is simple to understand. In my project there are too many html lines lying between them.

<?php 
    $id = 11;
 ?>
 <?php 
    echo '<script> window.location.href = "index.php?id=$id"</script>';
  ?>

It should take me to same page with like "index.php?id=11" but it loads page as "index.php?id=". What i can do to over come this ?

Faizan Ahmad
  • 355
  • 3
  • 14

2 Answers2

2

Try the following:

<?php $id = 11; ?>
<?php 
    echo '<script> window.location.href = "index.php?id='.$id.'"</script>';
?>
Vagabond
  • 877
  • 1
  • 10
  • 23
1

single quoted strings do not expand vars (https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single), check the Note.

Your example works this way:

<?php
    $id = 11;
 ?>
 <?php
    echo '<script> window.location.href = "index.php?id=' . $id . '"</script>';
?>
Mattias
  • 11
  • 2