title: Data from local storage modified when passing to php
body: <p>I'm trying to write a form that on first submition writes form fields to local storage. I have a button that reads the data back from local storage. I want to pass this data to php so it can then be posted back into the form.</p>
  <p>The data is json encoded, when I try and pass the data to php it is being corrupted and not recognised as json encoded.</p>
  <p>I'm sending to local storage:</p>
  <pre><code>$encodedString= json_encode( array());
  
  &lt;script type=&quot;text/javascript&quot;&gt;
    // Access the elements of the array
    var passed_array =[];
    var passed_array = ('&lt;?= $encodedString; ?&gt;');
    localStorage.setItem('&lt;?= $hostname; ?&gt;',JSON.stringify(passed_array));
  &lt;/script&gt;
  </code></pre>
  <p>To retrieve from local storage:</p>
  <pre><code>&lt;script&gt;
      var starray = localStorage.getItem('&lt;?= $hostname; ?&gt;');
      //document.write(starray);
  &lt;/script&gt;
  </code></pre>
  <p>if I output starray from inside the script using</p>
  <pre><code>document.write(starray);
  </code></pre>
  <p>It is valid json data and can be decoded, I'm feeding the output into:
  <a href="https://json_decode.onlinephpfunctions.com/" rel="nofollow noreferrer">https://json_decode.onlinephpfunctions.com/</a> to test.</p>
  <p>When I try and pass the json data into a php so I can &quot;json_decode&quot; it, it is being corrupted and not seen as valid json format.
  I'm passing to php using:</p>
  <pre><code>$starray = &quot;&lt;script&gt;JSON.parse(document.write(starray));&lt;/script&gt;&quot;;
  </code></pre>
  <p>The output from <code>document.write(starray);</code> is valid json:</p>
  <pre><code>{&quot;hostname&quot;:&quot;aaaa&quot;,&quot;loopback_ip&quot;:&quot;10.255.69.83&quot;,&quot;upstream_device&quot;:&quot;&quot;,&quot;aapr_int_ip&quot;:&quot;&quot;,&quot;aapr_bundle&quot;:&quot;&quot;,&quot;aapr_int&quot;:&quot;&quot;,&quot;linkid_west&quot;:&quot;&quot;,&quot;aapr_exchange_west&quot;:&quot;&quot;,&quot;ring_id&quot;:&quot;&quot;,&quot;be_ip&quot;:&quot;10.255.104.217&quot;,&quot;uplink_bundle&quot;:&quot;10&quot;,&quot;phy_int&quot;:&quot;&quot;,&quot;ospf_area&quot;:&quot;&quot;,&quot;oob_ip&quot;:&quot;&quot;,&quot;exchangename&quot;:&quot;&quot;,&quot;siteaddress&quot;:&quot;&quot;,&quot;city&quot;:&quot;&quot;,&quot;state&quot;:null,&quot;floor&quot;:&quot;&quot;,&quot;suite&quot;:&quot;&quot;,&quot;rack&quot;:&quot;&quot;,&quot;user_id&quot;:&quot;&quot;} 
  </code></pre>
  <p>The output from <code>$starray = &quot;&lt;script&gt;JSON.parse(document.write(starray));&lt;/script&gt;&quot;;</code> is NOT valid json:</p>
  <pre><code>string(53) &quot;{&quot;hostname&quot;:&quot;aaaa&quot;,&quot;loopback_ip&quot;:&quot;10.255.69.83&quot;,&quot;upstream_device&quot;:&quot;&quot;,&quot;aapr_int_ip&quot;:&quot;&quot;,&quot;aapr_bundle&quot;:&quot;&quot;,&quot;aapr_int&quot;:&quot;&quot;,&quot;linkid_west&quot;:&quot;&quot;,&quot;aapr_exchange_west&quot;:&quot;&quot;,&quot;ring_id&quot;:&quot;&quot;,&quot;be_ip&quot;:&quot;10.255.104.217&quot;,&quot;uplink_bundle&quot;:&quot;10&quot;,&quot;phy_int&quot;:&quot;&quot;,&quot;ospf_area&quot;:&quot;&quot;,&quot;oob_ip&quot;:&quot;&quot;,&quot;exchangename&quot;:&quot;&quot;,&quot;siteaddress&quot;:&quot;&quot;,&quot;city&quot;:&quot;&quot;,&quot;state&quot;:null,&quot;floor&quot;:&quot;&quot;,&quot;suite&quot;:&quot;&quot;,&quot;rack&quot;:&quot;&quot;,&quot;user_id&quot;:&quot;&quot;}&quot; 
  </code></pre>
  <p>I can't figure out why it's being changed and how to fix it.
  Any help would be appreciated!</p>