title: sending javascript variable to php script via post without form
body: <p>I know there are quite a few questions like mine, but I'm attempting to pass a javascript variable to a php script using AJAX. Can anyone point out what I'm doing wrong? Can't seem to figure it out. My javascript on original page:</p>
  
  <pre><code>var myvariable = document.documentElement.innerHTML;
  // send variable to server via ajax here  
  $.ajax({
     url: 'myphp.php',
     data: { myvariable : myvariable },
     type: 'POST',
     success: function(result) {
      alert("success!");
         }
  });
  </code></pre>
  
  <p>and then to receive on the 'myphp.php' page:</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['myvariable']))
  {
  $myvariable = $_POST['myvariable'];
  }
  ?&gt;
  </code></pre>
  
  <p>I'm trying to do this without using a form submit - I just want it to send the variable via ajax on page load. Attempting to echo or print the variable $myvariable results in a blank page. Also trying to use this variable in an API call gives me a warning that the field is blank.</p>