I have a problem while passing a value of an element via $_SESSION
<?php
  session_start();
?>
<?php
  echo "<table>";
  $con = mysql_connect("localhost","root","") or die ("problem");
  mysql_query("SET NAMES 'utf8'", $con);
  mysql_select_db("dedomena");
  $query = mysql_query("SELECT * FROM posts");
  $i = 1;
  while($query_row = mysql_fetch_assoc($query)) {
    echo  "<tr><td onclick='myFunction($i)'><a
           href='page.php'>".$query_row['title']."</a></td><td>$i</td>";
    $i = $i + 1;    
  };
  echo "</table>";
?>
<script>
  function myFunction($i) {
    alert(x);
    <?php
      $_SESSION["id"] = $i;
    ?>
  }
</script>
The code on page2.php is
<?php
  session_start();
?>
<?php
  echo $_SESSION["id"];
?>
When I click on an element on the first page I get the right id
But when I try to pass the values to the second page it only shows me the number 10! 
Note: the number 10 is obviously because of the $i + 1 loop
but how do I fix that?
 
     
     
     
     
    