title: Ways to pass php post data to javascript
body: <p>So I was wondering, if I have a simple php form that posts information to a certain variable, how can I pass that posted variable to javascript and be able to manipulate the data? The only way I've figured out is by using inline javascript with php but I feel there must be a cleaner and more elegant way to do it where the variables don't show up so blatantly in the source code and are handled externally.. Here's what I'm playing around with right now:</p>
  
  <pre><code>&lt;form action="index2.php" method="post"&gt;
      &lt;input name="sugar" class="form" type="text" value="" /&gt;
      &lt;input name="fat" class="form" type="text" value="" /&gt;
      &lt;input name="protein" class="form" type="text" value="" /&gt;
      &lt;input type="submit" value="submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Which is followed by:</p>
  
  <pre><code>&lt;?php 
  $sugar = $_POST['sugar'];
  $fat = $_POST['fat'];
  $protein = $_POST['protein'];
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          var sugar = "&lt;?php echo $sugar ?&gt;";
          var fat = "&lt;?php echo $fat ?&gt;";
          var protein = "&lt;?php echo $protein ?&gt;";
      &lt;/script&gt;
      &lt;script src="test.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;/head&gt;
  </code></pre>
  
  <p>Does anyone have any suggestions for alternatives I could follow? I haven't been using javascript for too long so it would be nice to know my options when dealing with a situation such as this.</p>