Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON encode in Javascript and decode in PHP
    primarykey
    data
    text
    <p>I create an object in Javascript to pass as the argument to a PHP script.</p> <pre><code>var pattern = new Object(); pattern['@id'] = ''; pattern['@num'] = ''; pattern.cprop = new Object(); //pattern.aprop = new Object(); pattern['@id'] = id; pattern['@num'] = pn; pattern.cprop.$ = pb.find('input[name="subject"]').val() || ''; var json = {'pattern': pattern}; </code></pre> <p>My Ajax call is</p> <pre><code>url: clr_url_base+'storepat.php?data='+encodeURIComponent($.toJSON(json)) </code></pre> <p>In my PHP script, I use</p> <pre><code>$pat = json_decode(str_replace ('\"','"', $data), true); $prep = $pat["pattern"]["@id"]; $sense = $pat["pattern"]["@num"]; $cprop = $pat["pattern"]["cprop"]["$"]; //$aprop = $pat["pattern"]["aprop"]["$"]; </code></pre> <p>This works, but when I add the <strong>aprop</strong> value, it no longer works.All values are strings. Any suggestions as to what's going wrong.</p> <p>Here are the two JSON strings:</p> <pre><code>{\"pattern\":{\"@id\":\"against\",\"@num\":\"1(1)\",\"cprop\":{\"$\":\"a person or thing (often abstract) opposed\"}}} {\"pattern\":{\"@id\":\"against\",\"@num\":\"1(1)\",\"cprop\":{\"$\":\"a person or thing (often abstract) opposed\"},\"aprop\":{\"$\":\"verbs (to which \'against\' introduces idea of opposition); nouns denoting conflict\"}}} </code></pre> <p>The first has only has the value for <strong>cprop</strong>, while the second adds the value for <strong>aprop</strong>. Note that <strong>aprop</strong> has single-quotes. It is this kind of data that seems to beg for encoding in the Javascript and decoding in the PHP. The second bombs. I have some 20 fields from a form, so it would get quite complex to create the JSON by hand, rather than as fields in <strong>pattern</strong>. When the second bombs, the value of <strong>$pat</strong> is NULL.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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