0

I have several divs, which are made automatically by php script. Each div have and record from mysql database in it. And hidden input with value of unique ID. What I wan't to do is when I click on each of this divs, I would like to open overlay with div in middle and use this id to acces another data in database. I have working overlay. I just don't know how to pass that variable.

It's something like this:

echo "<div class='friend'>".$a["username"]."<input type='hidden' value='$id'/>"."</div>";

I hope someone will understand what I'm asking for. :) I know it's terrible explanation.

0

3 Answers 3

1

Your clean code should be this:

echo '<div class="friend"> ' . $a["username"] . '<input type="hidden" value="' . $id . '"/></div>';

But way more cleaner would be having a list of your friends like this:

echo '<ul>';
echo '<li><a href="#' . $id . '" class="friend">' . $a["username"] . '</a></li>';
echo '</ul>';

When using jQuery for example you can handle click events with a few lines and pass your user id to an external PHP like this:

$('.friend').click(function(e) {
  e.preventDefault();

  $.get('/userDetails.php?uid=' + $(this).attr('href'), function(data) {
    alert(data);
  });
});
1
  • This is great I just have one problem I can't trigger overlay with it. It does not work at all. Any idea why, I put any div id or class as a trigger and it works, perfect. But than i put .friend and nothing. Commented Mar 19, 2012 at 21:02
1

Something like this should work:

$('div.friend').click(function(){
   val = $(this).find("input").val();

   //open your overlay with the value as a parameter
});
2
  • And what this will do? I need to get that id and put it to php variable? Anyway thank you for quick answer. Commented Mar 19, 2012 at 20:42
  • You will then need to use ajax to call your php script and pass the val variable as GET or POST variable. I assumed you were already doing this because you said that you already had the overlay working. Commented Mar 19, 2012 at 20:44
0

This?

       echo "<div class='friend' onclick='(function(id){ $(\'#overlay\').html(id).show(); }(\'$id\'));'>".$a["username"]."<input type='hidden' value='$id'/>"."</div>";

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.