最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

GET id from php with javascript - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Apr 1, 2014 at 16:16 Mike Dunker 1,99015 silver badges17 bronze badges asked Apr 1, 2014 at 15:56 PabloArtyPabloArty 431 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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){

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.

发布评论

评论列表(0)

  1. 暂无评论