3

Morning! I'm having problems with my code. I need to click an image and get an overlay with the information of that image. I have the overlay and my image sends the ID.

My php is:

echo "<td><a href='#?id=".$row['id']."'' class='qwe'><img src='../covers/nocover.jpg' width='100' /></a></td>";

And my JavaScript is:

$( ".qwe" ).click(function() {
  $.get("Get.php", function(respuesta){
      $("#popup").html(respuesta);
  })
  $( "#popup" ).show("fade");
}); 

my Get.php is:

<h1>You sent a value by GET: <?php echo $_GET['id']; ?></h1>

when i click my anchor class .qwe my overlay works good, it opens and show me the Get.php file inside but the problem is that i don't know how to catch the id that my anchor is sending.

2 Answers 2

3

Assign the element with an id

<a id='".$row['id']."' href='#?id=".$row['id']."'' class='qwe'

Read the id with this.id and you can send it up

$( ".qwe" ).click(function() {
   var id = this.id;
   $.get("Get.php", { "id" : id }, function(respuesta){

if you really want to not set the id of the element, you can read the attr("href") and append it to the url.

$( ".qwe" ).click(function() {
   var qs = $(this).attr("href")
   $.get("Get.php" + qs, function(respuesta){
Sign up to request clarification or add additional context in comments.

2 Comments

I took your answer and I did this:<a id='?id=".$row['id']."' href='#' class='qwe'... And works!! thank you bro!
id can't start with number in HTML4, something to keep in mind; perhaps data-id attribute would be a better pick.
2

If you set the id attribute on the a element it will be accessible with JS

echo "<td><a id='". $row['id'] ."' href='#?id=".$row['id']."'' class='qwe'><img src='../covers/nocover.jpg'     width='100' /></a></td>";

And then in the JS:

$( ".qwe" ).click(function() {
    var id = this.id;

Alternatively, if you don't want to use the id attribute (as Jack mentioned, you can't have an id attribute's value beginning with a number in HTML4) you could use HTML5's data- attributes and access these with jQuery's .data() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.