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 06-01-2011, 08:47 PM   PM User | #1
steve4891
New to the CF scene

 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
steve4891 is an unknown quantity at this point
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!!
steve4891 is offline   Reply With Quote
Old 06-02-2011, 09:10 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
"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);
devnull69 is offline   Reply With Quote
Old 06-02-2011, 09:19 AM   PM User | #3
steve4891
New to the CF scene

 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
steve4891 is an unknown quantity at this point
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
steve4891 is offline   Reply With Quote
Old 06-02-2011, 06:10 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,461
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 06-04-2011, 06:41 PM   PM User | #5
steve4891
New to the CF scene

 
Join Date: Jun 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
steve4891 is an unknown quantity at this point
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?
steve4891 is offline   Reply With Quote
Old 06-05-2011, 03:19 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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.
devnull69 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 01:41 AM.


Advertisement
Log in to turn off these ads.