Search code examples
phphref

How do I get the ID of a selected HREF tag in PHP?


I have multiple href tags each with a different ID. Now, I want to get the ID of the selected HREF tag I click. How can I do this in PHP?

Thanks


Solution

  • It is not possible to do that in PHP. You must use JavaScript and pass through GET parameters the ID of the clicked link.

    If you have jQuery, It'd be like this:

    $("a").click(function(e) {
        e.preventDefault();
        var a = $(this);
        window.location.href = a.attr('href')+"?clicked_id="+a.attr('id');
     });
    

    So in your PHP code, you can use $_GET['clicked_id'] to identify which one was clicked.

    But the simpler way, is to generate unique URL's for each <a> tag. So, You can determine which URL was clicked without any JavaScript code, which, in this case, seems to be desnecessary.