[nycphp-talk] Passing JAVASCRIPT variables to PHP
John Campbell
jcampbell1 at gmail.com
Tue Apr 1 10:18:59 EDT 2008
I can think of a half a dozen solutions for sending data from
javascript back to the server, below are three.
1) Have javscript set a cookie:
<script>
var clienttime = ... // long caluculation
setCookie('ct',clienttime,365);
</script>
2) Have javascript send the data as a get variable (you could also
auto submit a form if you want POST)
<script>
var clienttime = ... // long caluculation
window.location = "myscript.php?ct=" + clienttime;
</script>
3) Ajax assuming jQuery
<script>
var clienttime = ... // long caluculation
$.get('myscript.php',{ct:clienttime},function (data) {
alert("script said: " + data);
});
</script>
The cookie method will make the data available to the Server on the
next page view.
The second method will auto redirect and be really sloppy, but it is
the easiest to understand conceptually.
The Ajax method is the best, but will be a real pain in the ass to
implement without a javascript library.
Even though the second method is the worst from a UI perspective, I'd
recommend you implement it first so you better understand the problem.
-John Campbell
More information about the talk
mailing list