title: Sending data from JavaScript to PHP via XMLHttpRequest
body: <p>Good day.</p>
  
  <p>I'm trying to send a simple piece of data from one php file (manage.php) to another (view.php).</p>
  
  <p>I cannot send the data via a form, I want to send it via a JS script. Here's my attempt:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var read = function(id) {
    xmlhttp = new XMLHttpRequest();
  
    xmlhttp.open("POST", "view.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("id=" + id);
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>In view.php, using $_POST["id"] causes an error stating that the index "id" is undefined.</p>
  
  <p>What's the correct way to send the data? Thank you.</p>