0

I'm new to PHP and I'd like to detect if a link is clicked, to know what link it is and then to execute some code.

Something like that:

<body>
<a href="page.php" class="link1"></a>
<a href="page.php" class="link2"></a>
<a href="page.php" class="link3"></a>

<?php
    if (link1 clicked){
        //do some code
    }
    if (link2 clicked){
        //do some other code
    }
    if (link3 clicked){
        //do some other code
    }
?>
</body>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Could you give an example of a link and what do you want to happen when it is clicked? PHP runs on a server so it generally has no idea what a user does on the screen. – Dharman Sep 27 '19 at 18:36
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Dave Sep 27 '19 at 19:21

3 Answers3

0

If these links must be to the same page then you'll need to send some piece of information to that page. Consider a query string value:

<a href="page.php?link=1" class="link1"></a>
<a href="page.php?link=2" class="link2"></a>
<a href="page.php?link=3" class="link3"></a>

Then in your server-side code (in page.php, not in the page which is rendering this HTML) you can conditionally check that value:

$link = $_GET["link"];
if ($link == "1") {
    //do some code
} else if ($link == "2") {
    //do some other code
} else if ($link == "3") {
    //do some other code
}

Depending on the logic being performed (what "some code" and "some other code", etc. are), you may also want a default action to be performed when link isn't provided at all or is an unexpected value. Could be as simple as an error response back to the user.

Note that, also depending on what you're doing, you may want to move these operations to their own pages instead of one big catch-all page. But that's more theoretical at this point and may be outside the scope of the question being asked.

David
  • 208,112
  • 36
  • 198
  • 279
0

PHP will only execute at runtime. You are unable to execute different code for the same page without passing through some kind of attribute or variable to the page.

For example:

<body>
<a href="page.php?link=link1"></a>
<a href="page.php?link=link2"></a>
<a href="page.php?link=link3"></a>
</body>

Then, on page.php you would determine which link had been clicked.

<?php
// page.php
    if ($_GET['link'] == 'link1'){
        //do some code
    }
    if ($_GET['link'] == 'link2'){
        //do some other code
    }
    if ($_GET['link'] == 'link3'){
        //do some other code
    }
?>
0

Client-side vs Server-side scripting

Here, it is important to understand the difference between client-side scripting and server-side scripting. PHP code runs on the server, so once this code gets called, it will do server-side stuff like database queries and generate the relevant HTML code and sends it to the user's browser (client-side) and the PHP code's role ends here.

Now to track user's browser behavior while the page is open, you will need Javascript code, that runs on the user's browser (client-side), and tracking user clicks can be easily done using Javascript's onclick event:

$(".link").on("click", function() {
  console.log(this.id + " has ben clicked");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="page.php" id="link1" class="link">Link 1</a>
<a href="page.php" id="link2" class="link">Link 2</a>
<a href="page.php" id="link3" class="link">Link 3</a>

Or, using PHP:

If you want your page.php script to see which link called it, you need to pass some extra parameter to the url, as Alex W. mentioned in his answer, so your URL would become something like:

page.php?callerLink=1

And then you can get this paramter from PHP, you would use something like:

$callerLink = $_GET["callerLink"];
Anis R.
  • 6,656
  • 2
  • 15
  • 37