3

I have the following code in a file called one.php

<?php
session_start();
$_SESSION['one'] = "ONE";
$_SESSION['two'] = "TWO";
?>

<html>
<body>
<a href="two.php">Click here for 1.</a>
<a href="two.php">Click here for 2.</a>
</body>
</html>

Now when I click on one of the anchor tag links, it takes me to two.php.

Here I want the output to be,

One was clicked. OR Two was clicked.

How do I make the above output in two.php using PHP?

Parixit
  • 3,739
  • 3
  • 38
  • 59
Kemat Rochi
  • 893
  • 3
  • 18
  • 35
  • have you tried calling your `session` on `two.php` file? – Sam Teng Wong Mar 09 '16 at 05:43
  • use `$_GET` or `$_POST` to pass variables – Parixit Mar 09 '16 at 05:43
  • `$_SESSION` variables are globals, what means you would be able to retreive the value simply by calling `$_SESSION` and its given key, `$_SESSION['one']` – ImAtWar Mar 09 '16 at 05:45
  • 1
    Possible duplicate of [How to pass variables between php scripts?](http://stackoverflow.com/questions/5678567/how-to-pass-variables-between-php-scripts) – Ravi Mar 09 '16 at 05:52

4 Answers4

4

The simplest solution is to append a paramter to the url

<a href="two.php?clicked=1">Click here for 1.</a>
<a href="two.php?clicked=2">Click here for 2.</a>

then in PHP

if (isset($_GET['clicked'])) {
      $clicked = (int)$_GET['clicked'];
} else {
      $clicked = 0;
}
Robbie
  • 17,323
  • 4
  • 33
  • 71
1

code is here,

 <?php

    if($_REQUEST['click'] == 1)
    {
        echo "One was clicked.";
    }
    else if($_REQUEST['click'] == 2)
    {
        echo "Two was clicked.";
    }

?>

<html>
<body>
<a href="two.php?click=1">Click here for 1.</a>
<a href="two.php?click=2">Click here for 2.</a>
</body>
</html>
solanki
  • 208
  • 1
  • 4
  • 17
0

You can pass variables by using GET or POST method.

Here is simple GET (query string) method.

main.php :

<html>
<body>
<a href="two.php?btn=One">Click here for 1.</a>
<a href="two.php?btn=Two">Click here for 2.</a>
</body>
</html>


two.php :

<?php
if(!empty($_GET['btn'])) {
    echo $_GET['btn'] . " was clicked";
}
?>
Parixit
  • 3,739
  • 3
  • 38
  • 59