This has been driving me nuts for a few days. I am desperate for help.
I have 2 files, index.php and test.php, simplied version to follow.
index.php
<div id="div-hi"></div>
<div id="div-bye"></div>
<script>
$(document).ready(function(){ setInterval(function(){
$( function()
{
$.ajax(
{
url : 'test.php',
type : 'post',
success : function( resp )
{
$('#div-hi').html(resp);
$('#div-bye').html(resp);
}
});return false;}
);}, 1000);});
</script>
test.php
<div id="hi">
<script type="text/javascript">
---
</script>
</div>
<div id="bye">
<script type="text/javascript">
----
</script>
</div>
I want #div-hi in index.php to contain the result of my javascript from #hi from test.php and #div-bye from index.php to contain the result from #bye from test.php.
I have ran out of ideas. Please help.
Your Javascript:
You’ve got a few extra brackets and parens, and I don’t know what the
return falseis doing in this case…The format for setInterval() is
using an anonymous function with
$.ajax()that’s:Also,
return falseis usually used to cancel defualt behavior in jQuery…. it doesn’t apply here.If your response is HTML, you can extract a div from it with
find()or a context.Try something like this:
Working jsFiddle
( note how the
{}s appear after a few secs )Extracting DIVs from a page:
Use the
.load()function if you simply want to load pieces of another page onto the current page.For example if you’re on
index.phpand you want to grab the contents of#hiand#byefromtest.phpand display them in#div-hiand#div-byeThe above is two calls though, but I just wanted to illustrate the use of
.load().