AliDK AliDK - 4 years ago 99
HTML Question

only first value is accessible in range of value returned by php

I have php function which retrieve users information from database and set them in 5 variables. 4 of them are visible and one of them is hidden. when I run the function, list of users is display correctly. And I want to access the hidden variable with jquery on click event, (to test jquery work fine I try to alert the value of hidden variable) but when I click on each user in list it display the value of hidden variable which is belong to the first user in the list.

there is my php code (only the important part):

$contacts->bind_result($contactID, $name, $lastName, $photo, $emotion);
while($contacts->fetch())
{
$this->setContactID($contactID);
$this->setName($name);
$this->setlastName($lastName);
$this->setPhoto($photo);
$this->setEmotion($emotion);
?>
<!-- this part display photo, name and last name and emotion of contacts. -->

<div style="height:60px">
<div style='margin-top:5px; margin-right:5px; display:inline; float:left'>
<?php echo "<img src=.." ,$this->getPhoto()," height='45' width='45' />" ?>
</div>
<div style="display:table; padding-top:5px; padding-bottom:10px">
<?php echo $this->getName() . " " . $this->getlastName(); ?>
</div>
<div style="display:table">
<?php echo $this->getEmotion(); ?>
</div>
<input type="hidden" class="contId" value="<?php echo $this->getContactID(); ?>" />
</div>

<?php

}


I use (.load) function in jquery to load this php function in a div with (contacts) id in my html and also I use jquery ui in this div to make each user in list selectable. and I want to access each user hidden value when they are selected in list.

and this is my Jquery code:

$("#contacts").load("displayContact.php");

$("#contacts").selectable({
selected:function(){alert ($(".contId").val());}
});

Answer Source

I would take advantage of the callback events. This is what I would do:

$("#contacts").selectable({
    selected: function (event, ui) {
        var id = $(ui.selected).find(".contId").val();
        alert(id);
    }
});

source: http://api.jqueryui.com/selectable/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download