How Can I Pass A Variable From Javascript To PHP?

Welcome Login | Register

How Can I Pass A Variable From Javascript To PHP?
  Forums Index
      » PHP Forums
        » » How can I pass a variable from Javascript to PHP?



How Can I Pass A Variable From Javascript To PHP?
Post Description:
Post Tags: , how, can, i, pass, a, variable, from, javascript, to, php, free php support, php scripts, php questions, php answers, programming, codes, scripts
This Post Has Been Viewed 526 Times Since Thu Feb 21, 2008 8:48 am Posted By hostman with 0 replies
Next Post »» PHP htmlentities vs htmlspecialchars differences php functions
How Can I Pass A Variable From Javascript To PHP?
this is a good question i had.. i was able to find this:

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.

<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
  // output the geometry variables
  echo "Screen width is: ". $_GET['width'] ."<br />n";
  echo "Screen height is: ". $_GET['height'] ."<br />n";
} else {
  // pass the geometry variables
  // (preserve the original query string
  //   -- post variables will need to handled differently)

  echo "<script language='javascript'>n";
  echo "  location.href="${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
            . "&width=" + screen.width + "&height=" + screen.height;n";
  echo "</script>n";
  exit();
}
?>



Leave Your Comments






Share
HTML Link:
You can use this HTML code to put it on your website to show your friends this wallpaper. Use this code on your profile like myspace, friendster, Facebook or others. Just Copy and Paste it in your HTML on your websites



Fourms BBCODE:
BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums





Comments and replies About How Can I Pass A Variable From Javascript To PHP?




(0) Comments