Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 09:18 PM.

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #5
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <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??

  • #6
    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
    Did you define the variable: rootdomain

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •