tags:

views:

55

answers:

2

Hello, I am trying to pass an id value to a php script with jquery.

So, I was trying to create a link:

<a href="#" id="superman">Superman</a>

and when this link is clicked, I would like jquery to grab the id value and place it in the php script or php grabs the id? im not sure the best way.

I want the id value so it can be placed in a mysql query like this:

SELECT * FROM hero_db WHERE category="superman"

So, i get the id and place it in for the category as seen above.

I have trying many things, but am unfamiliar with jquery but trying to learn as much as I can.

Thank you for any help on this.

+2  A: 

It sounds like you want to load HTML when the link is clicked. If so you need markup something like this:

<a href="#" id="superman" class="loaddata">Superman</a>

<div id="superman_data">
</div>

with Javascript:

$("a.loaddata").click(function() {
  $("#" + this.id + "_data").load('/loaddata.php', {id: this.id});
  return false;
});

What this does is binds an event listener to all anchors with a class of "loaddata". When it's clicked on, the ID is passed to loaddata.php. That script should return HTML. It is loaded into the named div.

There are various jQuery Ajax methods. Which one you use depends on what you want to achieve.

cletus
A: 
$.post('http://PHP_SCRIPT_URL', {id: $('a.yourlik').attr('id')});
Andrew Kolesnikov