title: Getting undefined array key when passing data with XMLHttpRequest
body: <p>I'm trying to pass a long formatted text string to a php process to save to a file. I am use the POST method as the string can be quite long. It seems to run thro the process and gives back the message 'File saved' but with the error message 'Undefined Array Key &quot;data&quot;' on line 2 of the php code. The file does not get saved and no data is being sent across from the XMLHttpRequest in the previous function.
  My code is:- This gets 'str' from the previous function and is formatted correctly.</p>
  <pre><code>function getGame(str){
      //Sends data to the php process &quot;saveGame.php&quot;.
      var data = str;
      var xhr = new XMLHttpRequest();
      xhr.open(&quot;POST&quot;, &quot;saveGame.php&quot;, true);
      xhr.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
      
      xhr.onreadystatechange = function() {
          if (this.readyState === 4 ){
              alert(xhr.responseText);
          }
      };
      xhr.send(data); 
  } 
  </code></pre>
  <p>it then goes the the 'saveGame.php' where the problem occurs on line 2.</p>
  <pre><code>&lt;?php
  $outputString = $_POST[&quot;data&quot;];
  
  $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
  $fp = fopen(&quot;C:\\xampp\\htdocs\\BowlsClub\\GamesSaved\\test26.txt&quot;,&quot;w&quot;);
  fwrite($fp, $outputString);
  if (!$fp){
      $message = &quot;Error writing to file. Try again later!&quot; ;
  }else{
      $message = &quot;File saved!&quot;; 
  }
  fclose($fp);
  
  echo $message;
  ?&gt;
  </code></pre>
  <p>I think my code of the process is okay but there is a problem with passing the &quot;data&quot; variable and I not sure what it is.</p>
  <p>I tested the file saving process. I put a dummy string as a value of 'outputString' and it saved it. When I went back and used the current code the file was overwritten and was blank, indicating it had saved a blank string. So it seems no data is been passed to saveTeams.php although the saving of the file works.</p>
  <p>I have got a reproducible example below. If you use it with the saveTeams.php file and a file to attempt to save the data to it should display the error I get in an alert drop down.</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  
  
  &lt;head&gt;
  &lt;title&gt;Test program&lt;/title&gt;
  
  
  &lt;script language=&quot;javascript&quot;&gt;
  
  function formatOutput() {
      // lots of formatting goes on here
      
      var formattedText = &quot;This is a test&quot;;   //test data               
      getGame(formattedText);
  }
  
  function getGame(str){
      //Sends data to the php process &quot;save Game&quot;.
      var data = str;
      var xhr = new XMLHttpRequest();
      xhr.open(&quot;POST&quot;, &quot;saveGame.php&quot;, true);
      xhr.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
      
      xhr.onreadystatechange = function() {
          if (this.readyState === 4 ){
              alert(xhr.responseText);
          }
      };
      xhr.send(data); 
  }
  
  
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;table width=&quot;1000&quot; align=&quot;center&quot; border=&quot;0&quot;&gt;
          &lt;br /&gt;
          &lt;tr&gt;
              &lt;td width=&quot;500&quot;align=&quot;center&quot; colspan=&quot;3&quot; &lt;button onclick=&quot;formatOutput()&quot;/&gt;Click on me&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
      
      
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  <p>Hope this okay. I'm a bit new to this.</p>