-2

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?

Dave Moten
  • 11,957
  • 2
  • 40
  • 47
Kyriakos Anemos
  • 55
  • 1
  • 10
  • 2
    This is just so wrong !! – brute_force Oct 03 '15 at 11:03
  • What `id` would you expect to be printed on page2? Would it depend on which title was clicked? – Sjon Oct 03 '15 at 11:10
  • 1
    Please [don't use `mysql_*`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1); the `mysql_*` functions are outdated, [deprecated](http://us3.php.net/manual/en/intro.mysql.php), and insecure. Use [`MySQLi`](http://us3.php.net/manual/en/book.mysqli.php) or [`PDO`](http://us3.php.net/manual/en/intro.pdo.php) instead. – elixenide Oct 03 '15 at 11:12
  • find a tutorial on ajax.. – Rohit Kumar Oct 03 '15 at 11:30

4 Answers4

1
<script>
function myFunction($i) {
alert(x);
<?php
$_SESSION["id"] = $i;
?>
}
</script>

You are trying to include php code in javascript thinking you will change php global variable at client end when a click event happens. This is not the right way.

Sessions are for saving session variables. You are using sessions for passing GET params to server. Session variables can't be updated from client end since they are not exposed to client.

Sessions in PHP : The contents of the SESSION superglobal cannot be changed. This lives on the server and the client has no way to access this.

However, a session id is passed to the client so that when the client contacts the server the server knows which session to use. This value could be changed which would allow a user to use someone else's session.

brute_force
  • 1,141
  • 7
  • 12
1

As brute_force already mentioned, the problem is

<?php
$_SESSION["id"] = $i;
?>

This code is executed server-side, before the page is sent to the user. That's the difference between PHP and javascript: javascript is executed at runtime, while the page is loaded, but the PHP in the page is executed in order to load the page, so the SESSION["id"] is set to 10 in your while-loop, and then, in the mentioned piece of code, put into the SESSION variable. In order to get the variable in the session at runtime, you must create a request to a PHP script on the server, which in turn puts it in the SESSION. Have a look at (jQuery) ajax, that's the easiest solution in this case.

Tempestas Ludi
  • 1,113
  • 9
  • 24
0

but what you are trying to do? php is server side language and javascript is client side. you must do a ajax call to pass the value ID.

Red Acid
  • 217
  • 1
  • 3
  • 14
0

You can not execute server side codes on client through JS . Coming to your problem you can send it to php via string query

** BEST SOLUTION FOR THIS PROBLEM IS STRING QUERY**

while($query_row = mysql_fetch_assoc($query)) {

    echo  "<tr><td><a
    href='page.php?id='.$i>".$query_row['title']."</a></td><td>$i</td>";
    $i = $i + 1;    
    }

page2.php

<?php
id=$_REQUEST['id'];
echo $id;
?>

Or if rigid to session use ajax. A cheat solution with iframe(without ajax) is below

<?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>";
?> 
function myFunction(i) {
alert(i);
var html='';
    html +='<div id="fakeDiv">';
    html +='<form action="setSession.php" method="post"  id="fakefrm" target="ifk">';
    html +='<input type="hidden" name="jsvar" id="jsvar" value="'+val+'"/>';
    html +='</form>';
    html +='</div>';
    html +='<iframe name='ifk'style='display:none;></iframe>';
    var c=document.createElement('div');
    document.body.innerHTML += html;
    document.getElementById('fakefrm').submit();
    document.body.removeChild(document.getElementById('fakeDiv'));
}

setSession.php

<?php
session_start();
$_SESSION['id']=$_POST['jsvar'];
?>

Add setSession.php in your project..

NOTE - In fact , if i see this answer in quick , i caste downvote this , but this answer is an instant relief for a guy who is unaware the name "ajax"

Rohit Kumar
  • 1,948
  • 1
  • 11
  • 16