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
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    XMLHttpRequest problem

    Hi everyone,
    hope someone can help because i can't understand why it doesn't work.
    i have this function to load a php file.

    function php_load(file,id,id_target) {
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    var xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    if(id == 0){

    xmlhttp.onreadystatechange=function()
    {

    if (xmlhttp.readyState==4){
    document.getElementById(id_target).innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET",file,true);
    xmlhttp.send();
    }
    else if(id == 1){

    xmlhttp.open("GET",file,false);
    xmlhttp.send();
    return xmlhttp.responseText;

    }
    }

    the id parameter can be 0 or 1 and identify if i want to write inside the innerHTML of a DIV (0) or if i just want to receive a value (1).

    everything is ok normally.. for example I can use the function to add a row in my database and the row is correctly added.

    The problem is that if i wanna update a table with the new row and i call the function load_php the table is loaded but with old data and not with the new row.

    to be able to load the new data i have to close all browser pages and load from beginnig all my project. Even though i refresh the page it doesn't work.

    i don't know if it's a problem of some cache or something... i can't understand why new data are not loaded simply calling the function..

    Anyone has an answer to my problem??

    thanks everyone!!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    "Cache" is BINGO

    Ajax 'GET' requests tend to use the browser cache ... so if you always GET the same URL the browser will tend to display the same content from the cache even if the URL content has changed.

    Solution: GET a slightly different URL by adding a random parameter like a timestamp
    Code:
    var timestamp = new Date().getTime();
    xmlhttp.open('GET', file + '&timestamp=' + timestamp, true);

  • #3
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for clarification..

    just one doubt. i just simply add the timestamp as you showed or i need to do something in the php file?

    thank you very much

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    just adding the queryString breaks cache from javascript since you are requesting a "different" file every time.

    you can also set a past-date "Expires" header in php, which would prevent caching as well.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks...

    problem solved, but now i have another problem.

    it's a utf-8 problem..

    if i load a string if a special character (for example ˛Ó¨Ŕý) string is not passed to php.

    i tried everything..

    setHeaderContent, header in php, every suggestion present in internet but nothing work..

    somebody has a concrete solution to solve this other problem?

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Where do you pass anything to PHP? It's not in your code

    But nevertheless ... you did not read extensively enough. Because if I google for "Ajax send characters" the first result I get is the correct answer: You have to run encodeURIComponent() on every single parameter you send to PHP.


  •  

    Posting Permissions

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