View Full Version : Reading javascript vars in php? possible?
slaskis
08-31-2002, 05:42 PM
I read a post about executing php in a formbutton and i come to think about a script i tried before, i wanted to make a information script that collects info about user (resolution, and everything).
Som things is not possible with just php so i use javascript but i don't want to reload the page to let php read the info and write it to mysql. So i want to make a variable in javascript and then php reads the variable directly(assuming that the var is already determined ofcourse). i tried the var in javascript and it didn't work, but i really think it should :)
What do you think?
mordred
09-01-2002, 12:19 AM
That you were really close at accomplishing your task - but failed nevertheless, because PHP, no matter how powerful it is, absolutely can't read any JavaScript variables whatsoever...
But when you think a little bit differently, you don't actually need PHP to read something from JavaScript - you need JavaScript to tell PHP something (sort of the other way round). So write a PHP script that writes a defined set of GET parameters into your MySQL database, and then you only need to call this script from within JavaScript. To avoid reloading the page, you can do this by creating an image object and setting its src attribute:
var sW = screen.availWidth;
var quasi = new Image();
quasi.src = "http://example.com/script.php?screenWidth=" + sW;
The result is that your script gets executed on the server and writes the GET intput into your database - and the use won't experience the reloading of the page.
hth
slaskis
09-07-2002, 09:30 AM
Thanks, it seems that it should work but i'm not really sure what you mean about writing the php script i've tried it like this:
<script language="JavaScript">
var sW = window.screen.availWidth;
var quasi = new Image();
quasi.src = "http://localhost/hit.php?screenWidth=" + sW;
</script>
<?
$sW = $_GET['screenWidth'];
echo"$sW";
?>
so this is how i understood your example
mordred
09-07-2002, 11:06 AM
No, that can't work, though I'm a little unsure as to what you actually want to accomplish.
A call to a PHP script by using the GET request issued through assigning a value to the image.src in JavaScript does not give you the possiblity to retrieve textual data, only image data is acceptable. In newer browsers, you can issue HTTP requests in a more sophisticated way and work on the resulting response, see http://www.mozilla.org/xmlextras/ for details.
But for your initial question the technique mentioned by me would suffice... try this:
page1.html:
var sW = screen.availWidth;
var quasi = new Image();
quasi.src = "http://example.com/test.php?screenWidth=" + sW;
and on test.php
$fp = fopen('track.txt', 'a');
fwrite($fp, 'screen width: ' . $_GET['screenWidth'] . "\r\n");
fclose($fp);
Upon each time you open page1.html in your browser, a text file called track.txt will be created/appended with the screen width variable. Instead of writing to a file you could insert this value into your mysql database.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.