views:

52

answers:

3

Good beautiful afternoon everyone,

I've been stalking the JQuery fourms trying to get a good clean solution to this problem, however, I can't seem to knock this one out.

I have a page that has multiple links with various attributes (these attributes will be pulled in from a database):

index.php

<html
<head>
<script type='text/javascript' src='header.js'></script>
</head>
<body>
<a href="#" class="link_click"id="12">My_Link_1</a>
<a href="#" class="link_click"id="21">My_Link_2</a>

<div id='my_container'> </div>

</body>
</html>

My header.js file has:

$(document).ready(function(){
    $('.link_click').click(function(){
      $("#my_container").load("classes/class.project.php", {proj: $(this).attr('id')} );
      return false;
  });
});

class.project.php is pretty simple:

<?php
echo "<div id='project_container'>project = ".$_POST['proj']." : end project</div>";
?>

This loads and passes the ID variable (which actually comes from a database) to class.project.php. It works fine for the first link click (either link will work). Once one link is clicked no other links with this div class will work. It feels like javascript loads the class.porject.php and it will not refresh it into that #my_container div.

I tried running this as suggested by peterpeiguo on the JQuery Fourm, with the alert box for testing wrapped inside .each.

Copy code

$(document).ready(function() {
     $('.link_click').each(function() {
        $(this).click(function() {
          alert($(this).html());
     });
  });
});

This seems to work fine for the alert box. But when applying it to .load() it does not reload the page with the new passed variable. As a matter of fact, it doesn't even reload the current page. The link performs no function at that point.

The example site can be viewed here: http://nobletech.net/gl/

A: 

Sounds like the request is getting cached to me.

Try this:

 $.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
});
mdm20
That does not seem to do the trick. But I will look at .ajaxSetup. I didn't think of that. Here's the new header.js:$.ajaxSetup ({ cache: false});$(document).ready(function(){ $('.sidebar_click').click(function(){ $("#main_container").load("classes/class.project.php", {proj: $(this).attr('projid')} ); });});My test page is located at http://nobletech.net/gl/. The links are named different, but the problem is the same. Thank you very much for your help, -cs
cstrzelc
Do you know if .ajaxSetup only applies to the JQuery $.ajax call? I am using .load? Maybe .load does not provide the right capability here?
cstrzelc
+3  A: 

I looked at the link you posted, and the problem is that when you're doing load you're replacing the elements on the page with new ones, thus the event handlers don't work anymore.

What you really want to do is target the load. Something like:

$("#project_container").load("classes/class.project.php #project_container", {proj: $(this).attr('projid')} );

This only loads stuff into the proper container, leaving the links and other stuff intact.

Ideally, the php script should only return the stuff you need, not the whole page's markup.

BTW- Caching shouldn't be an issue in this case, since .load uses POST if parameters are passed. You only have to worry about ajax caching with GETs

Infinity
Success, a little different variation, but you got me on the right track Infinity. It works now... http://nobletech.net/gl/ Thanks a lot, first time using this site and it has a great community. Is there a way I mark this question closed, or give credit to you?
cstrzelc
Glad to help.. you mark questions as resolved by clicking on the grey check mark next to each answer, and upvoting helpful answers is a good idea too
Infinity
Perfect - thanks
cstrzelc
A: 

Sorry but this might be completely wrong but after examining your XHR response I saw that you are sending back html that replaces your existing elements.

So a quick fix would be to also send the following in your XHR response (your php script should output this also):

<script>
$('.link_click').each(function() {
    $(this).click(function() {
      alert($(this).html());
 });
</script>
andreas
please explain the down vote if you may.
andreas
It's a really bad idea to reattach event handlers in every ajax response, the right way is to only update what's necessary in the page, leaving everything else as is.
Infinity
so it's a bad i idea but it is not wrong..to get things straight. Ok got it..."a bad idea will provide down votes". Just trying to provide a quick fix...bad idea i guess.
andreas
It's not really a quick fix either, he'd have to include the JS file in the PHP file, instead of leaving it in the head as it is. A quick fix is to just change the load call to target the right container, which is what I suggested. Also extraneous bugs may occur later if you reload the whole JS lib on every ajax response. Bad idea
Infinity
I don't think I did the down vote did I... Sorry just started using the site, need to read the FAQ still:) Thanks for all your help. I am no longer reloading the link code and loading the class.project.php data in a separate div. Works like a charm.
cstrzelc