The Netlobo logo - a Nevada desert landscape

Passing information from Javascript to PHP

A couple of ways to pass information from javascript to PHP

Published Aug 1, 2005 by lobo235
Last updated on May 19, 2009

When writing PHP pages you may run into situations where you need some information from javascript to be passed to PHP so you can manipulate it server-side. This is a topic of discussion in many PHP forums so I have decided to present my ideas on the subject. Recently I was writing a web application using PHP and I wanted to be able to store the user's timezone in my database. This can't be done with PHP alone; I knew I would have to use javascript to get the timezone offset for the user and somehow pass that information to PHP so I could store it in MySQL.

I thought of a couple of ways that my problem could be solved. I could get the timezone offset in javascript and then use javascript to set a cookie that I could then read in PHP. I used this method for a while but then ran into problems with paranoid users who turned off cookies. I decided that instead of using cookies I would add a hidden form field to my login screen where I could store the timezone offset. That way, when the user logged in, they would send me their timezone offset. There are users out there who turn off their javascript so I made sure there was a way to manually set the timezone in the user preferences area, but for those users who leave their javascript turned on, this saves them a lot of time.

For those of you who wish to try the cookie method, here it is:

<script type="text/javascript">
    var tzo=(new Date().getTimezoneOffset()/60)*(-1);
    setCookie("mytzo", tzo, 30);
</script>

This snippet of javascript will get the user's timezone offset and set a cookie called "mytzo" that lasts for 30 days. The timezone offset is stored as the number of hours difference from Greenwich Mean Time (GMT). This value can then be read by PHP and stored into MySQL or another database.

The other method I spoke of is to set a hidden form element to the timezone offset value. To implement this I created a javascript function called getTZO() that gets called when the submit button is clicked. It gets the timezone offset and stores it in the hidden input with the id of "mytzo" so it can get submitted to my PHP page. The code is as follows:

function getTZO( )
{
    document.getElementById('mytzo').value = (new Date().getTimezoneOffset()/60)*(-1);
}

Now, set the onclick attribute of your submit button to be onclick="javascript:getTZO();" and you should now be able to read the "mytzo" value using your PHP script you are submitting to and save it to MySQL. I prefer this second method because some users turn off their cookies. I am open to your feedback, how do you like to do it?

1 comment for this article.

del.icio.us logo add this article to del.icio.us! Other great Web Development and Programming articles on Netlobo.com:
Unobtrusive Javascript made easy
Javascript location.reload example
Making AJAX Easier