title: JSON.stringify to PHP Issues
body: <p>I'm trying to get information from a form on HTML using Javascript, convert it to a JSON string and then send it to a PHP server to post, however, I'm running into the issue of receiving the data.</p>
  <p>When the return value in the function addEmployee() is false, the PHP can grab the information but does not update the page.  Whereas if the return value is set to true, it would update the page, but is unable to get the data from client, I need it for it to both so I am confused.  I also tried implementing AJAX code, but it didn't seem to work.</p>
  <p>HTML Code:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Assignment 7&lt;/title&gt;
          &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; type=text/css&gt;
          &lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form id=&quot;theForm&quot; action=&quot;process.php&quot; method=&quot;post&quot;&gt;
              &lt;fieldset&gt;
                  &lt;legend&gt;Add an Employee&lt;/legend&gt;
                  
                  &lt;label for=&quot;firstName&quot;&gt;First Name&lt;/label&gt;
                  &lt;br&gt;
                  &lt;input id=&quot;firstName&quot; name=&quot;firstname&quot; type=&quot;text&quot;&gt;
  
                  &lt;br&gt;
                  &lt;label for=&quot;lastName&quot;&gt;Last Name&lt;/label&gt;
                  &lt;br&gt;
                  &lt;input id=&quot;lastName&quot; name=&quot;lastname&quot; type=&quot;text&quot;&gt;
  
                  &lt;br&gt;
                  &lt;label for=&quot;department&quot;&gt;Department&lt;/label&gt;
                  &lt;br&gt;
                  &lt;select id=&quot;department&quot; name=&quot;department&quot; id=&quot;department&quot;&gt;
                      &lt;option value=&quot;Engineering&quot;&gt;Engineering&lt;/option&gt;
                  &lt;/select&gt;
                  
                  &lt;br&gt;&lt;br&gt;
                  &lt;input id=&quot;submit&quot; type=&quot;submit&quot;&gt;
              &lt;/fieldset&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>JavaScript Code:</p>
  <pre><code>var employees = [];
  function generateID() {
     return Math.random().toString().slice(2, 10);
  }
  
  function addEmployee() {
    'use strict';
    var fname = document.getElementById(&quot;firstName&quot;).value;
    var lname = document.getElementById(&quot;lastName&quot;).value;
    var dep = document.getElementById(&quot;department&quot;).value;
    var id = generateID();
    
    for(var i=0; i&lt;employees.length-1; i++){
       if(employees[i].id === id) {
         id = generateID();
         i = 0;
       }
    }
    
    var employee = {
      id:id, 
      fname:fname, 
      lname:lname, 
      dep:dep, 
      count:employees.length+1
    };
  
    employees.push(employee);
  
    var JSONstring = JSON.stringify(employee);
  
      const xhr = new XMLHttpRequest();
      
      xhr.open(&quot;POST&quot;, &quot;process.php&quot;);
      xhr.setRequestHeader(&quot;Content-Type&quot;, &quot;application/json&quot;);
      xhr.send(JSONstring);
      return true;
  }
  
  function init() {
    'use strict';
    document.getElementById(&quot;theForm&quot;).onsubmit = addEmployee;
  }
  window.addEventListener(&quot;load&quot;, init);
  </code></pre>
  <p>PHP Code:</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;PHP&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;?php
              $request = file_get_contents(&quot;php://input&quot;);
              $arr = json_decode($request, true);
              $browser = get_browser(null, true);
  
              echo nl2br(&quot;Employee Added \r\n&quot;);
              echo nl2br(&quot;Name: &quot; . $arr[&quot;lname&quot;] . &quot;, &quot; . $arr[&quot;fname&quot;] .&quot;\r\n&quot;);
              echo nl2br(&quot;Department: &quot; . $arr[&quot;dep&quot;] .&quot;\r\n&quot;);
              echo nl2br(&quot;Employee ID: &quot; . $arr[&quot;id&quot;] .&quot;\r\n&quot;);
              echo nl2br(&quot;Hire Date: &quot; . date(&quot;D M j Y&quot;) .&quot;\r\n&quot;);
              echo nl2br(&quot;Total Employees: &quot; . $arr[&quot;count&quot;] .&quot;\r\n&quot;);
              echo nl2br($browser[&quot;browser&quot;] .&quot;\r\n&quot;);
          ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>I understand that I can grab the information directly from PHP but sending a JSON to the server is one of my assignment's requirements so I do not have a choice.  Please help me.</p>