title: POST data to php using vanilla javascript and get it back
body: <p>I am trying to send variable to php using javascript and trying to receive back and alert that variable. But <code>$_POST['data']</code> doesn't get a data that I pass and <code>$obj</code> is always null.</p>
  
  <p>If I just <code>echo "some text";</code> it alerts that.</p>
  
  <p>Here is my javascript code:</p>
  
  <pre><code>getMessageType('some data'); //function call
  
  function getMessageType(str)
  {
      data = {'key': str};
      var xhr = new XMLHttpRequest();
      var url = "test.php";
      xhr.open('POST', url, true);
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
      console.log(data); //Object {key: "some data"}
      xhr.send(data);
  
      xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 &amp;&amp; xhr.status === 200) {
            console.log(xhr.responseText);
          }
      };
  }
  </code></pre>
  
  <p>Here is my PHP code:</p>
  
  <pre><code>&lt;?php       
  $obj = $_POST['key'];
  echo $obj;
  </code></pre>
  
  <p>What I am doing wrong ?</p>
  
  <p>Any help is appreciated. Thanks</p>