Note that there are some explanatory texts on larger screens.

plurals
  1. POStruggling with json_decode
    primarykey
    data
    text
    <p>I have looked at many posts on this forum and still having problems, can't get my head round it.</p> <p>i am retreiving a php page through ajax which is posting a json object:</p> <pre><code>$.ajax({ url: myUrl, data: sendData, type: "POST", error: function(xhr, statusText, errorThrown){ // Work out what the error was and display the appropriate message }, success: function(data){ // data retrived ok var myData = data; // do something with data } }); </code></pre> <p>my json is like this (generated with js):</p> <pre><code>{"borough": {"id": "3"}, "cat":{"id": "5", "id": "47", "id": "98"}} </code></pre> <p>using firebug i have hecked and its getting passed across</p> <p>my problem is with the php, i cant seem to get the data json_decoded.</p> <pre><code>&lt;?php $catPost = $_POST['cat']; echo($catPost); $catData = json_decode($catPost, true); var_dump($catData); ?&gt; </code></pre> <p>the echo statement prints out [object Object]</p> <p>but the var_dump prints out null</p> <p>what am i doing wrong? how do i access the diffirent "id" values in the data????</p> <p>any help greatly appreciated.</p> <p>the test page can be seen at <a href="http://www.reelfilmlocations.co.uk/NEW%20Search/fullsearch_jq.php" rel="nofollow">http://http://www.reelfilmlocations.co.uk/NEW Search/fullsearch_jq.php</a></p> <p>Instructions for testing: (use thetext "Advanced search" under the select category dropdown to perform the ajax call also select a borough and somecategories topopulate the json object with some data</p> <p>EDIT:</p> <p>the json is created by the following script which reads values of selected tages from an array:</p> <pre><code>var sendData = {"borough":[], "cat":[]}; //alert('borough tagger add '+BoroughTagger.myIdArray[intIndex]); sendData.borough.push({"id":BoroughTagger.myIdArray[intIndex]}); $.each(CatTagger.myIdArray, function(intIndex, objValue) { alert('cat Tagger add '+CatTagger.myIdArray[intIndex]); sendData.cat.push({"id":CatTagger.myIdArray[intIndex]}); }); </code></pre> <p>which would create a json object like the following: (checked this in fiebug)</p> <pre><code>{"borough": {"id": "3"}, "cat":{"id": "5", "id": "47", "id": "98"}} </code></pre> <p>I am now using <code>stringData = JSON.stringify(sendData)</code> to send the data via ajax but there dosent seem to be any name in the post collections, the string is there but if i reference it using <code>$myData = $_POST;</code> which echos "ArrayArray" the json_decode errors with: Warning: json_decode() expects parameter 1 to be string, array given in D:\wamp\www\ReelFilm\NEW Search\getAdvSearch.php on line 7 </p> <p>so how do i get the ajax call to assign a name tothe sent sata so i can access it using <code>$_POST['myData']</code></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.
 

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