tags:

views:

118

answers:

1

Hello. I have a little page I've been working on. It's at: http://nait.jtlnet.com/~fpkj5v0r/programmer.php - and as you can see, a jquery chart shows up, like it's supposed to.

Now here's the problem.. which has ended up taking up 6+ hours of my time... when I click a link (which uses ajax to load up a new page in the same div), and then I try to go back to the chart by clicking the correct link, nothing loads up.

  • The steps to see this take place is. Load up page and see the chart.
  • Then go under the "Water Plant" heading and click "test". This will load up a new page into the div.
  • Now, click on the link, "View Stats". This will load back up the exact same page that contains the chart.. except no chart show up. The jquery doesn't seem to work here. I've heard about jquery having an AJAX problem, since it's only a div loading up and not the whole page, but I've never had any luck with the .live() jquery stuff.

Please, any help would be appreciated. I've tried different charts, all using jquery, thinking my jquery was just messed up, but it seems to be something else.

I can post the code.. but it's just your regular jquery code in the header, and div opening in the corresponding page.

Thanks!

Marcus

A: 

Ok, so you return the placeholder div from ajax.

The problem I'm seeing now is that there's nothing telling javascript to write in the graph.

My suggestion is to take your current document.ready and change it:

CURRENTLY:

$(document).ready(function() {
  // stuff that happens
});

TRY CHANGING TO:

function documentOnReady() {

var json =  
[ 
    {"dates":"24 feb","visitors":"5"}, 
    {"dates":"25 feb","visitors":"21"}, 
    {"dates":"26 feb","visitors":"14"}, 
    {"dates":"27 feb","visitors":"45"}, 
    {"dates":"28 feb","visitors":"20"}, 
    {"dates":"29 feb","visitors":"18"}, 
    {"dates":"01 mar","visitors":"9"}, 
    {"dates":"02 mar","visitors":"7"}, 
    {"dates":"03 mar","visitors":"42"}, 
    {"dates":"04 mar","visitors":"17"}, 
    {"dates":"05 mar","visitors":"15"}, 
    {"dates":"06 mar","visitors":"9"}, 
    {"dates":"07 mar","visitors":"15"}, 
    {"dates":"08 mar","visitors":"3"}, 
    {"dates":"09 mar","visitors":"3"}, 
    {"dates":"10 mar","visitors":"19"}, 
    {"dates":"11 mar","visitors":"15"}, 
    {"dates":"12 mar","visitors":"11"} 
] ;

    var plot_data = new Array(); 
    var plot_ticks = new Array(); 

    for (var i in json) { 
        i = parseInt(i); 
        plot_data.push([i, json[i].visitors]); 
        plot_ticks.push([i+0.5, json[i].dates]); 
    } 

    $.plot($("#placeholder"),  
         [ 
            {"data": [[0, 0]]},  
            {"data": [[0, 0]]},  
            {"data": [[0, 0]]},  
            {"data": [[0, 0]]},  
            {"data": [[0, 0]]},  
             { 
                 label: "Last 20 days visits", 
                 bars: {"show": "true"}, 
                 data: plot_data 
             } 
         ], 
        { 
             xaxis: { 
               ticks: plot_ticks 
            } 
         } 
     ); 
}
$(document).ready(documentOnReady);

THEN in the callback of the ajax, you need to call documentOnReady() again to re-run the graph rendering code.

JBristow
ya, I was adding a page which contained my header, into the div... .At one point I was just loading up 3 lines of html code, one of them being a div which was linked to the chart, and that wouldn't work, so I decided to add the header to the div page. I know that was probably wrong, but I was trying different things to get it to work.
Marcus
Check my edited answer for further help.
JBristow
hello. I see what you're saying, so I just put the ajax content back to how it should be, with only a div inside: "<div id="placeholder" style="width:600px;height:300px;"></div>"this div is supposed to have a chart generated in it, via my jquery. But it still doesn't run since it seems to not see the newly created content... any ideas what I should do now?
Marcus
You need to have your javascript that puts the ajax response into your page ALSO run the code that generates the graph. Otherwise, all you'll have is an empty div.
JBristow
alright, yes, I get what you mean, and I do agree that I was trying to figure out a way to continually run that ajax code. So, I just tried what you said, and no dice. maybe i messed up, but I did exactly what you said. I put the jquery in the function, and I called it at the end of the ajax. I'll leave it as is, so you can look at the code behind if you want. I appreciate your help and your time, God knows I've been working on this 1 single bug for many many hours, and I was dreading today.
Marcus
you know the funny thing... when I throw a popup in there, the chart shows up... I was thinking maybe the ajax was too fast... any ideas? I'll leave the pop up in there for you to see it working with that.
Marcus
Just looking at this immediately, I can see that you've got the $(document).ready inside the function. You don't need that, so just get rid of that line, and its corresponding "});" at the end of the function.
JBristow
oh, ok, I took that out and I have it exactly how you want it. I took back out the pop up, and it doesn't work again. Hmmm. I hope you see something else that might be causing this. This is the luck I've been having lately. I'll try anything...
Marcus
hello. after implementing what you told me to, I had this: else if(str == "stats") { $('#content').html('<p><img src="images/ajax-loader.gif"></p>'); var url="ajax/generatepage.php"; url=url+"?q="+str; xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 } } xmlhttp.open("GET",url,true); xmlhttp.send(null); documentOnReady(); }I just tried putting the function call in the if statement and it seemed to work... not sure why though...
Marcus
that paste job looks convoluted, but all you have to see is this part:if(xmlhttp.readyState == 4 documentOnReady(); }Adding that function call is the code that made it work. I first added it after the if statement, and no dice. Thanks for everything!
Marcus