PHP and JavaScript interaction
In that article we’ll talk about peculiarities of working with JavaScript in PHP. In contrast to PHP scripts of JavaScript are run in the client side. PHP is server programming language. In comparison with Java or ASP.NET technology it doesn’t consist of any methods of working on the client side. That’s why if you want to create effective web applications you have to use PHP scripts as well as JavaScript.
There are two ways of such kind of interaction: variable transferring from JavaScript to PHP and dynamic forming of the scripts of JavaScript by means of PHP.
Variable transferring from JavaScript to PHP
One of the most important tasks is defining of the monitor dimension and color depth by means of JavaScript. That data will be transferred to the PHP script.
Script of the JavaScript is situated in the index.html file. Its content is listed below:
index.html file
Script Language="JavaScript">
var height=0;
var width=0;
colorDepth = screen.colorDepth;
if (self.screen)
{
width = screen.width
height = screen.height
}
else if (self.java)
{
var jToolKit = java.awt.Toolkit.getDefaultToolkit();
var scrsize = jToolKit.getScreenSize();
width = scrsize.width;
height = scrsize.height;
}
if (width > 0 && height > 0)
{
window.location.href = "http://localhost/view.php?
width=" + width +
"&height=" + height +
"&color=" + colorDepth;
} else exit();
</Script>
After that code has been run view.php page loads. Monitor dimension and color rendition depth can be seen in the browser.
view.php file
<?php
echo "Width : ".$_GET["width"];
echo "Heights: ".$_GET["height"];
echo "Color <sup>dimension</sup> : ".$_GET["color"];
?>
Working with JavaScript scripts is similar to working with data which are sent by means of method GET



