user547794
user547794

Reputation: 14521

JQUERY AJAX loading content w/ PHP loop

I have PHP loop that loops through a bunch of user IDs.

<?php while ($row = mysql_fetch_assoc($result)) : ?>
<?php $id = $row['id']; 

 <td width="90px" class="resultsDisplay"><a href=userdetail.php?id=<?php echo $id ?>> <?php echo $row['Username']; ?></a></td>

I would like to replace the HTML link there with an AJAX call to load that page in a particular DIV. The problem I'm having with the code below is that after a link is clicked if continues passing the rest of the user IDs for the loop. So, I click on a link, and it begins cycling through all of the user IDs in the database, loading about 70 pages in a row.

jQuery(document).ready(function(){
$(".resultsDisplay").click(function() {
  $.ajax({
   url: "userdetail.php?id=<?php echo $id ?>",
   success: function(msg){
     $("#results").html(msg);
   }
 });
});

Upvotes: 0

Views: 1657

Answers (2)

jeroen
jeroen

Reputation: 91792

You seem to be mixing the initial page-load with an ajax request. How are you generating your document.ready function?

What seems definitely wrong is your use of php in the document.ready function; for the ajax call to work, the ID you are sending to userdetail.php needs to be grabbed from the specific link / div that was clicked, not a static ID that was generated at page-load.

Upvotes: 0

ifaour
ifaour

Reputation: 38135

I really didn't get your question but I suppose you are outputting more than one ID? so you have more than one TD..etc?
The way you should do this (what ever you are doing) is like this:

jQuery(document).ready(function(){
$(".resultsDisplay a").click(function() {
  $.ajax({
   url: $(this).attr("href"),
   success: function(msg){
     $("#results").html(msg);
   }
 });
 return false;
});

Also you are missing the quotes for your href attribute.

Upvotes: 3

Related Questions