How do I pass JavaScript variables to PHP? How do I pass JavaScript variables to PHP? php php

How do I pass JavaScript variables to PHP?


You cannot pass variable values from the current page JavaScript code to the current page PHP code... PHP code runs at the server side, and it doesn't know anything about what is going on on the client side.

You need to pass variables to PHP code from the HTML form using another mechanism, such as submitting the form using the GET or POST methods.

<DOCTYPE html><html>  <head>    <title>My Test Form</title>  </head>  <body>    <form method="POST">      <p>Please, choose the salary id to proceed result:</p>      <p>        <label for="salarieids">SalarieID:</label>        <?php          $query = "SELECT * FROM salarie";          $result = mysql_query($query);          if ($result) :        ?>        <select id="salarieids" name="salarieid">          <?php            while ($row = mysql_fetch_assoc($result)) {              echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one)            }          ?>        </select>        <?php endif ?>      </p>      <p>        <input type="submit" value="Sumbit my choice"/>      </p>    </form>    <?php if isset($_POST['salaried']) : ?>      <?php        $query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid'];        $result = mysql_query($query);        if ($result) :      ?>        <table>          <?php            while ($row = mysql_fetch_assoc($result)) {              echo '<tr>';              echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others              echo '</tr>';            }          ?>        </table>      <?php endif?>    <?php endif ?>  </body></html>


Just save it in a cookie:

$(document).ready(function () {  createCookie("height", $(window).height(), "10");});function createCookie(name, value, days) {  var expires;  if (days) {    var date = new Date();    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));    expires = "; expires=" + date.toGMTString();  }  else {    expires = "";  }  document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";}

And then read it with PHP:

<?PHP   $_COOKIE["height"];?>

It's not a pretty solution, but it works.


There are several ways of passing variables from JavaScript to PHP (not the current page, of course).

You could:

  1. Send the information in a form as stated here (will result in a page refresh)
  2. Pass it in Ajax (several posts are on here about that) (without a page refresh)
  3. Make an HTTP request via an XMLHttpRequest request (without a page refresh) like this:

 if (window.XMLHttpRequest){     xmlhttp = new XMLHttpRequest(); }else{     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } var PageToSendTo = "nowitworks.php?"; var MyVariable = "variableData"; var VariablePlaceholder = "variableName="; var UrlToSend = PageToSendTo + VariablePlaceholder + MyVariable; xmlhttp.open("GET", UrlToSend, false); xmlhttp.send();

I'm sure this could be made to look fancier and loop through all the variables and whatnot - but I've kept it basic as to make it easier to understand for the novices.