Home page
 
 Home 
 ASP 
 PHP 
 SQL 
 HTML 
 JavaScript 
 Search 
 Contact 
 
Search
or browse popular tags
Got questions?

We got answers.

Ask your question about PHP, ASP, Javascript, HTML, CSS, hosting and get professional help, free of charge.

Access Functions
Subscription

Sign up for the free email newsletter for new tips, tutorials and more. Enter your email address below, and then click the button.

Privacy Policy

RSS Twitter

Passing JavaScript variables to PHP

Print Bookmark and Share

JavaScript is mainly used as a client side scripting language, while PHP is a server side technology. Unlike Java or ASP.Net, PHP doesn't have tools to make it work client side. That is why you need to combine JavaScript and PHP scripts to develop powerful web-applications.

One of the frequent problems is defining visitor’s screen resolution using JavaScript tools and passing this data to PHP-script. The following script provides solution for this problem:

<script type="text/javascript">

width = screen.width;
height = screen.height;

if (width > 0 && height >0) {
    window.location.href = "http://localhost/main.php?width=" + width + "&height=" + height;
} else 
    exit();

</script>

Copy and paste this code snippet in the text editor, save it as index.htm and run it in your browser. After this code has been executed, a user is automatically redirected to the main.php page where screen resolution is displayed in the browser window.

The main.php looks as follows:

<?php
echo "<h1>Screen Resolution:</h1>";
echo "Width  : ".$_GET['width']."<br>";
echo "Height : ".$_GET['height']."<br>";
?>

As you can see, passing JavaScript variables in PHP is similar to sending data using GET method.

Free Contact Form

Need help with ASP, PHP, Javascript?

Let our experts help you.
 
 
PHPRunner. Build great looking PHP+MySQL web sites with no programming
Web hosting (PHP,ASP,MySQL,SQL server,unlimited emails)



Tags: PHP JAVASCRIPT VARIABLES PASSING SCRIPTS GET

Add To: Add to dzone dzone | Digg this digg | Add to del.icio.us del.icio.us | Stumble it stumbleupon

  • Comments (14)





Copyright © 2005-2009             www.WebCheatSheet.com All Rights Reserved.