PDA

View Full Version : An easy Ajax fix? Please help!!!!


mryanmay
02-13-2008, 08:23 PM
Basically, what I want to happen is I want to have an empty div populate with a table displaying live statistics. I have the code written for that and basically want to call the .php page to refresh inside this div. Here is what I have:
_________________________________________________
<script language="javascript" type="text/javascript">
var url = location.href;

window.onload = setInterval('ajaxFunction();',10000);
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;

try{
// Opera, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// IE
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Please Try Your Request Again Later");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("POST", 'ajaxDiv', true);
ajaxRequest.send(null);
}

//-->
</script>

<div id="ajaxDiv">
</div>

______________________________________________________


So basically I want to inject my .php file inside the div and show its contents. Currently it injects the index page after the first 10 seconds. What would be great is if I could take the current URL and strip it of the file reference and replace it with the .php file. i.e.:

www.url.com.../admin/page.html
to
www.url.com.../admin/file.php

and then point the div to repopulate with the php url inside the div. It (www.url.com...) needs to be dynamic because this is on an enterprise app. What do you guys have?

shyam
02-13-2008, 09:08 PM
Basically, what I want to happen is I want to have an empty div populate with a table displaying live statistics. I have the code written for that and basically want to call the .php page to refresh inside this div. Here is what I have:
_________________________________________________
<script language="javascript" type="text/javascript">
var url = location.href;

window.onload = setInterval('ajaxFunction();',10000);
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;

...
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("POST", 'ajaxDiv', true);
ajaxRequest.send(null);
}

//-->
</script>

<div id="ajaxDiv">
</div>

the second argument to the open method is the url and ur passing the id of the div...so, ur div is probably getting populated with a 404

So basically I want to inject my .php file inside the div and show its contents. Currently it injects the index page after the first 10 seconds. What would be great is if I could take the current URL and strip it of the file reference and replace it with the .php file. i.e.:

www.url.com.../admin/page.html
to
www.url.com.../admin/file.php

and then point the div to repopulate with the php url inside the div. It (www.url.com...) needs to be dynamic because this is on an enterprise app. What do you guys have?

just give 'file.php' as the second argument in the open method and you should be fine...there is no javascript voodoo required :/

mryanmay
02-13-2008, 09:12 PM
so if I change the mydiv you referenced in red to:

rootdomain+'/Admin/ListStatsAjax.php
(or whatever the path is to the file I want to display)

Would that work?


j/k: that doesn't work........that is the type of thing I want to insert into that div though...

shyam
02-13-2008, 09:22 PM
so if I change the mydiv you referenced in red to:

rootdomain+'/Admin/ListStatsAjax.php
(or whatever the path is to the file I want to display)

Would that work?

as long as u are on the same domain from which the page was served...generally its advisable to use relative urls only

mryanmay
02-13-2008, 09:34 PM
<script language="javascript" type="text/javascript">
var url = location.href;

window.onload = setInterval('ajaxFunction();',10000);
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;

...
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("POST", rootdomain+'/Admin/ListStatsAjax.php ', true);
ajaxRequest.send(null);
}

//-->
</script>

<div id="ajaxDiv">
</div>

__________________________________________________

So when I make that change, nothing happens. I mean no content is refreshed or shown. With the part in red changed to its original 'ajaxDiv' it at least brought up the index....any other ideas??

A1ien51
02-14-2008, 03:55 PM
Did you define the variable: rootdomain

Eric