Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-13-2008, 07:23 PM   PM User | #1
mryanmay
New to the CF scene

 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mryanmay is an unknown quantity at this point
An easy Ajax fix? Please help!!!!

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?
mryanmay is offline   Reply With Quote
Old 02-13-2008, 08:08 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Quote:
Originally Posted by mryanmay View Post
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

Quote:
Originally Posted by mryanmay View Post
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 :/
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 02-13-2008, 08:12 PM   PM User | #3
mryanmay
New to the CF scene

 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mryanmay is an unknown quantity at this point
Maybe?

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...

Last edited by mryanmay; 02-13-2008 at 08:18 PM..
mryanmay is offline   Reply With Quote
Old 02-13-2008, 08:22 PM   PM User | #4
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Quote:
Originally Posted by mryanmay View Post
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
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 02-13-2008, 08:34 PM   PM User | #5
mryanmay
New to the CF scene

 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mryanmay is an unknown quantity at this point
<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??
mryanmay is offline   Reply With Quote
Old 02-14-2008, 02:55 PM   PM User | #6
A1ien51
Senior Coder

 
A1ien51's Avatar
 
Join Date: Jun 2002
Location: Between DC and Baltimore In a Cave
Posts: 2,717
Thanks: 1
Thanked 94 Times in 88 Posts
A1ien51 will become famous soon enough
Did you define the variable: rootdomain

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:30 PM.


Advertisement
Log in to turn off these ads.