-1
<script type="text/javascript">
func1(){};
func2(){};
</script>

<body>
<a href="javascript:func1();">some text</a>
<a href="javascript:func2();">some text</a>
</body>

How do I know which link is clicked in php?

1 Answers1

0

Php is a server-side language. If you want to let the server know that the client clicked on a link, you could make an ajax-request.

<html>
    <body>
        <script type="text/javascript">
            function callAjax(url, callback){
                var xmlhttp;
                // compatible with IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function(){
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
                        callback(xmlhttp.responseText);
                    }
                }
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }

            function func(id){
                callAjax('some_file.php?id=func'+id, function(res){alert(res);})
            };
        </script>

        <a href="javascript:func(1);">some text</a>
        <a href="javascript:func(2);">some text</a>
    </body>
</html>

some_file.php:

<?php
    $id = @$_GET['id'];
    if (!isset($id))
        $id = "";
    echo "You clicked on link " . htmlspecialchars($id, ENT_QUOTES, 'UTF-8') . ".";
?>
swift
  • 1,108
  • 7
  • 9
  • **Danger**: This code is [vulnerable to XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)). User input needs escaping before being inserted into an HTML document!. – Quentin Jul 27 '15 at 08:33
  • Ye but it was just a quick example (he's probably testing this with localhost anyway). But fine, I've added htmlspecialchars now to prevent xss. – swift Jul 27 '15 at 08:38