1

I'm a bit new to Ajax. I've got a Ajax file which includes a php file and refreshes it every X seconds. That code for AJAX.PHP is:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script language="JavaScript">
setInterval( "SANAjax();", 10000 );  ///////// 10 seconds 
$(function() {
SANAjax = function(){

$('#dataDisplay').fadeOut("slow").load('rand.php').fadeIn("slow");

}
 });
</script>
<div id="dataDisplay"></div>

This code is working fine. Bbut the html of AJAX.PHP page shows as it is code, while I want the output html of rand.php not javascript code given above. How can i do it?

suppost "rand.php" html out put (source) is: < html >< body >this is body <\ body ><\ html >.

I want this html to be shown on the html source of AJAX.PHP. How can i do it? If I mess any thing let me know, I'll try to make it more clear.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
Nandla
  • 207
  • 3
  • 13

2 Answers2

0

You need to run it. Not insert only as file.

$.post("rand.php", { },
function(data) { $('#dataDisplay').html(data) });

updated:

SANAjax = function(){
$.post("rand.php", { },
function(data) {  $('#dataDisplay').fadeOut("slow").html(data).fadeIn("slow"); });
}
Jirka Kopřiva
  • 2,939
  • 25
  • 28
  • Thanks for the help, Can u update above code with the changes ??? I mean where to insert this line in above ajax script ? – Nandla Mar 03 '12 at 13:02
  • look here: http://stackoverflow.com/questions/5687600/jquery-call-ajax-every-10-seconds – Jirka Kopřiva Mar 03 '12 at 13:08
  • Thank you for the update. But it is still showing javascript code instead of plain html source code. I want output without displaying actual javascript code in source of html. – Nandla Mar 03 '12 at 14:51
  • than oly the reason can be - APACHE is not running. – Jirka Kopřiva Mar 03 '12 at 14:59
  • the code is being executed and rand.php output is clear on the page but when I see its source code, there is no output but only code there – Nandla Mar 03 '12 at 15:21
  • You can't see it in the source code. It's written dynamicaly. Use FIREBUG or any debug tool to see the actual content. – Jirka Kopřiva Mar 03 '12 at 15:36
  • I think I'm still unable to clear what I really want :( I've a webstie say example.com and a page say ajax.php, I call ajax.php in a div of example.com through ajax, I want to include ajax.php output source code (html) into example.com source code, not only file name in javascript, but source code – Nandla Mar 03 '12 at 15:40
  • I want my ajaxed file readable for search engines, how can i do it? – Nandla Mar 03 '12 at 15:43
  • Then you have to generate the page on server side. (For exapmple via CRON, which will repeat the runing php script.) Javascript is handling with visual outputs. Not for rendering of content. – Jirka Kopřiva Mar 03 '12 at 15:47
  • If I refresh whole page for updated score card every 30 seconds, It will make my server out of resources, thats why i want to include a file which need to be updated every 30 seconds, but I want that file to be included in parent page source code as well. I'm unable to figure this out. Thank you for your time btw – Nandla Mar 03 '12 at 15:53
  • It doesn't make a sense. If you need to have content for search engine, you need to have it in the page at the moment. You can't prepare it via script. Its a show for web browsers. Search engine bot is not waiting and reading page as in browser. Neither is running any scripts. So you can update each page reload. Its the same result for usability you are writing about. – Jirka Kopřiva Mar 03 '12 at 16:07
  • but people are doing so like cricwaves . com – Nandla Mar 03 '12 at 16:12
  • But not with effect you expect. – Jirka Kopřiva Mar 03 '12 at 16:21
-2
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_rand').load('rand.php').fadeIn("slow");
}, 10000); // refresh every 10000 milliseconds
</script>

<div id="load_rand"> </div>

Don't forget to add the jquery library

Raphael1
  • 84
  • 3
  • 12