简体   繁体   中英

Loading an external PHP file that contains Javascript

I am trying to get the following to work. I have this code in my footer file:

footer.php:

<script type="text/javascript">
$(document).ready(function(){
$.ajax({ 
url: '', 
type: 'GET', 
dataType: "script",
success: function(data){ 
  //data is returned back 
  $('#latestforumposts').html(data); 
} 

});

}); 
</script>

And this is the file forumposts.php:

<script type="text/javascript" src='http://www.habboxforum.com/external.php?forumids=1382,1384,4,5,14,1410,7,85,40,43,124,123,24,30,306,34,446,38,214,1409,249,767,69,71,73,134,56,45,1230,54,135,1424,1425,601,893,209,1086&type=js'></script>

<script type="text/javascript">
<!--
for (x = 0; x < 5; x++)
{
document.writeln("<tr class='forumnewposts'><td><div class='forumpostwidth'><a target='_blank' href='http://www.habboxforum.com/showthread.php?t="+threads[x].threadid+"'>"+threads[x].title+"</a></div></td><td><div class='forumuserwidth'>"+threads[x].poster+"</div></td></tr>");
}
//-->
</script>

Basically, the file doesn't seem to load in to the div #latestforumposts like it is meant to yet if you visit the forumposts.php page directly it shows the content like it is supposed to (eg the script in forumposts.php works).

How can I drag that script and place it in a div on my page. The reason I need to do this is I need it to auto-refresh every x number of seconds.

UPDATE:

Okay, so I now have the following code in my footer:

<script type="text/javascript" src="http://www.habboxforum.com/external.php?forumids=1382,1384,4,5,14,1410,7,85,40,43,124,123,24,30,306,34,446,38,214,1409,249,767,69,71,73,134,56,45,1230,54,135,1424,1425,601,893,209,1086&type=js"></script>
  <script type="text/javascript">
$(document).ready(function(){
$.ajax({ 
url: '', 
type: 'GET', 
dataType: "html",
success: function(data){ 
  //data is returned back 
  $('div#latestforumposts').html(data); 
} 

});

}); 
</script>

This now loads the script but instead of displaying the html that is loaded in to the div#latestforumposts it just refreshes the whole page content with it. Any idea why it doesn't load in to the div?

jQuery

$(document).ready(function()
{
    update(false);
});

function update(load)
{
    if(load)
    {
        $.get(url,function(data)
        {
            $('#latestforumposts').html(data); 
        });
    }

    setTimeout(function(){update(true)},5000);
}

PHP (/forumposts.php)

<script type="text/javascript" src="http://www.habboxforum.com/external.php?forumids=1382,1384,4,5,14,1410,7,85,40,43,124,123,24,30,306,34,446,38,214,1409,249,767,69,71,73,134,56,45,1230,54,135,1424,1425,601,893,209,1086&type=js"></script>
<script type="text/javascript">
    for(x = 0; x < 5; x++)
    {
        $('#new_posts tbody').append('<tr class="forumnewposts"><td><div class="forumpostwidth"><a target="_blank" href="http://www.habboxforum.com/showthread.php?t='+threads[x].threadid+'">'+threads[x].title+'</a></div></td><td><div class="forumuserwidth">'+threads[x].poster+'</div></td></tr>');
    }
</script>
<table id="new_posts"><tbody></tbody></table>

HTML

<div id="latestforumposts"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM