Search code examples
phpsessionposthref

set SESSION variable when clicking an <a href>


I have a php script which prints href links on the page like this:

//...
echo"<link><a href=\"nextpage.php\">$table_list[$i]</a><br>";
//...

$table_list has table names from my database, and I want to go to nextpage with say $_SESSION['tablename']=$table_list[$i];

How can I do it? Thanks.


Solution

  • You can pass the value you want by appending a query string to the link. Something like:

    echo "...nextpage.php?tablename=$table_list[$i]...";
    

    Then in nextpage.php, grab the value of $_GET['tablename'] and put it in the user's session.

    $_SESSION['tablename'] = $_GET['tablename'];