0

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);
   }
 });
});
1
  • Can you provide a little more context. How is this being written to the page? I'm guessing that you are looping in your php and outputting this and that would not work. Commented Jan 1, 2011 at 17:30

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, that's right - Each <td> has it's own user ID which is gathered by looping through the database. With your code do I change anything with my <td> code (besides adding quotes to href?) I still need to pass in the userID somehow.
no the idea is to find a way to pass the user ID to your ajax call which can be achieved by my code above.
please note that the jQuery(document).ready is not closed..assuming you have more code under the click event..!
Thanks, that worked beautifully! I also did forget to paste the end of the document.ready, good eye.
I have found that the return false; in this code is breaking an upload script I have further up on the page. Any idea why that's happening?
|
0

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.

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.