Bishwaroop Chakraborty Bishwaroop Chakraborty - 3 months ago 14x
PHP Question

Subsequent anchor tags not working in html/php

I am trying to alert out individual id of every user on the user link click in html. So, every user is being fetched from the database and displayed on the page as a link. Also, since they have a unique email id, I am trying to alert out their emails when that user link is clicked. My issue is whenever I click on the first one, it alerts out the email, but the second, third or so on, on clicking does not pop up any alert.

Here is a snippet of the code :

<strong><a href="#" id="post_user" name="post_user" class="<?php echo $messagePost_Email; ?>"><?php echo $messagePost_FirstName.' '.$messagePost_LastName;?></a></strong>


<script type="text/javascript">

var element = $(this);
var emailID = element.attr("class");
var info = "emailID="+emailID;
tpye : "POST",
url : "navigate.php",
data: info,
success : function(){



Any suggestions would be of great help since I am not able to figure out the root cause of this behaviour.


The issue is that you're probably using the same id attribute for every anchor element:

<strong><a href="#" id="post_user" ...

The id attribute is meant to be an identifier and as such must be unique.

You should get rid of the id attribute (if you don't use it elsewhere) and as a jQuery selector use class instead. Eg:

<strong><a href="#" class="post_user" ...

The emailID (your user id) store in a data attribute of the element as it's more clear way of doing such things. This is what the data attribute is best to use for.

<a href="#" class="post_user" data-emailID="<?php echo $messagePost_Email; ?>" ...


        var emailID = $(this).attr("data-emailID");
        //  or:
        //  emailID = $(this).data("emailID");
            type    : "POST",
            url     : "navigate.php",
            data    : {emailID : emailID},
            success : function(response){
                // ... do anything with "response" ...