Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON Encode/decode doesn't work as it should
    primarykey
    data
    text
    <p>I am preparing and sending a JSON string from my PHP file to my Javascript function like this:</p> <pre><code>$json = array(); $json['slice'] = false; $json['G500'] = false; $json['KG1'] = false; $encoded = json_encode($json); die($encoded); </code></pre> <p>However, in my JS function, if I do this, it is unable to decode the JSON object:</p> <pre><code>var d = req.responseText; var jsonObject = eval(d); </code></pre> <p>The only way, I can get it to <code>eval</code> the JSON object is by adding parentheses manually</p> <p><code>jsonObject = eval("(" + d + ")");</code></p> <p>I have the same problem going in reverse as well. Sending a JSON object to PHP and trying to decode it there fails. I believe I would need to remove the parentheses in my PHP script before attempting to decode.</p> <p>Why is this happening? Is there something I can do to work around this incompatibility?</p> <p>EDIT:</p> <p>PHP to JS is now working if I use <code>JSON.parse</code>. I'm still having trouble the other way around.</p> <p>This is how I'm sending the data to the PHP:</p> <pre><code>var JSONstring = { "Product": document.getElementById('item').value, "Size": document.getElementById('size').value, "Quantity": document.getElementById('quantity').value }; url = "maintainOrder.php?json=" + JSON.stringify(JSONstring); req.open("GET", url, true); </code></pre> <p>However, the PHP script is unable to decode it.</p> <pre><code>$newItem = json_decode($_GET['json']); array_push($_SESSION['order'],$newItem); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload