开发者

Using AJAX call to a PHP page issue

开发者 https://www.devze.com 2023-01-10 04:09 出处:网络
I\'m working on an assignment which determines the client timezone offset , passes the same to a PHP script, which contains a code to fetch the timezone identifier for that client\'s area.

I'm working on an assignment which determines the client timezone offset , passes the same to a PHP script, which contains a code to fetch the timezone identifier for that client's area.

Now for determining timezone offset, I have used JavaScript. However, I need some way to pass this to PHP. I used AJAX call for the same.

var url = "index.php";
url=url+"?client_timezoneoffset="+offset;
xmlhttp.open("GET",url,true);
xmlhttp.send(true);

This JavaScript I'm calling onload of index.php. But still $_GET comes empty. I twisted this implementation and called another page set_client_timezone.php where to my surprise, $_GET contained the timezone. I then stored it in $_SESSION.

When I did an all-round testing of the application, to my surprise, the timezone changes were getting reflected on SECOND load of the page.

开发者_StackOverflow中文版

i.e. first time I navigated to http://www.example.com/index.php, it showed me time in UTC, but when I reloaded, it showed me correct time as per my timezone - Asia/Calcutta.

Can anyone of you guys out there suggest me a way out of this paradox ?

Can anyone point me a standard way of achieving the same (I'm ready to rollback my current implementation)

I'm posting in this forum for the first time and so heartfelt apologies if my post flouted any rule of this forum.

Cheers,

Abhijit.


If you change the timezone by ajax, then the php serverside stuff for loading the index.php on first call is already finished. Then after the index.php is finished the client side/javascript code starts Ajax request and sets the timezone into the session, so for the next reload the serverside php stuff knows the correct timezone.

To fix it you could try it the other way round. Set the timezone in php and pass it to your javascript code.


I agree with above suggestion. You can involk the js function after loading the main content(index.php) for solve this issue.

setTimeout("yourfunction()",1000);// load function after 1 seconds from the actual loading time

This may helps you.


I just reworked on the loading basics of PHP and JavaScript and re-altered my code to make it work. Thanks all for the prompt feedback.

Anyone looking for a good and excellent solution to this issue, I direct you to a blog - http://www.incoherent.ch/blog/2008/02/20/php5-time-zone-solution/

Cheers,
Abhijit

0

精彩评论

暂无评论...
验证码 换一张
取 消