"; echo "Screen height is: ". $_GET['height'] ."<br />"; } else { // pass the geometry variables // (preserve the original query string // -- post variables will need to handled differently) echo "<script language='javascript'>"; echo " location.href="${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}" . "&width=" + screen.width + "&height=" + screen.height;"; echo "</script>"; exit(); } ?>">

How can I pass a variable from Javascript to PHP?

  • Вебдизайн для начинающих

Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables. It is, however, possible to pass variables between the two. One way of accomplishing this is to generate Javascript code with PHP, and have the browser refresh itself, passing specific variables back to the PHP script. The example below shows precisely how to do this -- it allows PHP code to capture screen height and width, something that is normally only possible on the client side.