Passing JavaScript Variables to PHP

Passing JavaScript Variables to PHP

This is a very tricky thing, as javascript is browser/user side, and php is serverside.

I know you can do it because i’ve done similar with ajax in the past. (myabe i just did request i don’t remember)

Anyway, does anyone know how to pass javascript embeded in a page to a remote servers php code?

Thanks SitePoint

It depends upon the context.

At which point is JavaScript supposed to send variables to PHP?

Is the PHP script meant to output something? If so, is the page sending the variables meant to output this somewhere?

Assuming you’re talking about a non-AJAX implementation, and retrieving the variables from Javascript after a page reload… Use the javascript to write out a value into a hidden form field, and use PHP to retrieve it from the $_POST.

PHP is meant to receive the request and input it into a database. :slight_smile:

Is there a specific reason you don’t want to do this through a normal form?

Also, are you using any JavaScript libraries such as JQuery? They make AJAX requests easier, so they’re useful.

Im trying to create a google analytic’s type app

so i want it super light to reduce pageloads so no jquery or other library containing non essential content

and cant use a standard form because the script will be included on remote webpages and is supposed to require no action on the users behalf

(this post just got moved from the PHP part of the forum)

(new Image).src = "http://......?foo=" + encodeURIComponent(foo);

So make it think its an image and pass the variables to it?

if so thanks man, ill give it a try :slight_smile:

(new Image).src = "http://......?foo=" + encodeURIComponent(foo);