0

I am new to php I have jquery/javascript based application which involves finding nearby places and give the result in a sorted order .I am trying to separate the sort function from html page to a server side php.

  1. It involves sending and ajax request to "http://thewebby.net16.net/SortJson.php" with json array as parameter

  2. Which parses the json request and sorts it

Based On these SO Question and Answer

Sending JSON to PHP using ajax

Parsing JSON file with PHP

Jquery

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/SortJson.php",
        data: {myData:datas},
        success: function(data){
            alert('Items added');
        },
        error: function(e){
            console.log(e.message);
        }
});

PHP

<?php

if(isset($_POST['myData'])){
 $obj = $_POST['myData'];
 //some php operation
 $jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($obj, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}
}else{
 echo "File Working";
}
?>

The page is hosted at http://thewebby.net16.net Upon checking the network activity during search for Objects Button the ajax call is supposed to be made.But its not happening when i checked network activity in chrome developer tool

JSON DATA

[{"place":"IDBI Bank ATM","Distance":0.615,"Lat":8.531954,"Lng":76.92878100000007},{"place":"South Indian Bank ATM","Distance":0.662,"Lat":8.52868,"Lng":76.92865400000005},{"place":"IndusInd Bank","Distance":0.919,"Lat":8.525279,"Lng":76.928992},{"place":"SBT ATM","Distance":1.118,"Lat":8.522826,"Lng":76.92875000000004},{"place":"SBT ATM","Distance":0.7,"Lat":8.525756,"Lng":76.92017099999998},{"place":"Axis Bank ATM","Distance":0.771,"Lat":8.538183,"Lng":76.92333299999996},{"place":"Canara Bank ATM","Distance":0.662,"Lat":8.530059,"Lng":76.929123},{"place":"Indian Overseas Bank ATM","Distance":0.651,"Lat":8.528429,"Lng":76.92841399999998},{"place":"State Bank ATM","Distance":0.924,"Lat":8.539249,"Lng":76.92552699999999},{"place":"Corporation Bank ATM","Distance":1.019,"Lat":8.524513,"Lng":76.92949099999998},{"place":"Catholic Syrian Bank","Distance":0.715,"Lat":8.529943,"Lng":76.92959100000007},{"place":"United Bank of India ATM","Distance":1.015,"Lat":8.529396,"Lng":76.93226000000004},{"place":"Canara Bank","Distance":0.656,"Lat":8.530155,"Lng":76.92908699999998},{"place":"State Bank Of India e-Corner","Distance":0.824,"Lat":8.538184,"Lng":76.92589099999998},{"place":"punjab national bank ATM","Distance":0.625,"Lat":8.53369,"Lng":76.92835500000001},{"place":"STATE BANK OF TRAVANCORE ATM","Distance":0.964,"Lat":8.539663,"Lng":76.92535299999997},{"place":"Bank of Baroda","Distance":0.839,"Lat":8.529734,"Lng":76.930702}]

EDIT:-

$.post( "SortJson.php",{myData:datas} , function( data ) {
 alert('Items added'+data);

});

When $.post is used instead of $.ajax method its working,the request is being sent .But at the server side the php file is always entering else case of

if(isset($_POST['myData'])){..}
else{
 echo "File Working";
}

How to parse the following data in php ,I am getting Invalid argument error the passed variable is neither object or array.Am updating the code in the website with $.Post Instead of $.ajax

Json Data is sent as this

EDIT 2:-

Result Of var_dump

array (
  0 => 
  array (
    'place' => 'GreenPepper',
    'Distance' => '0.487',
    'Lat' => '8.52699',
    'Lng' => '76.92419100000006',
  ),
  1 => 
  array (
    'place' => 'BAKE \'N\' COOL',
    'Distance' => '0.513',
    'Lat' => '8.527908',
    'Lng' => '76.92643599999997',
  ),
)

For this json input

[{"place":"GreenPepper","Distance":0.487,"Lat":8.52699,"Lng":76.92419100000006},{"place":"BAKE 'N' COOL","Distance":0.513,"Lat":8.527908,"Lng":76.92643599999997}]
Community
  • 1
  • 1
codefreaK
  • 3,584
  • 5
  • 34
  • 65
  • Why are you decoding it twice? And you're going to need to show the code that is supposed to trigger the request. – rjdown Aug 07 '15 at 21:47
  • @rjdown i am very new to php am a java developer I wanted to move some coding to server side and test somethings so only free domain allows is php so am testing on php this as mentioned is the code from second Q&A mentioned above – codefreaK Aug 07 '15 at 21:49
  • You never encode your data as JSON. Your screenshot of the FormData shows that it is form encoded, not JSON encoded. What does JSON have to do with any of this? – Quentin Aug 08 '15 at 14:30
  • @Quentin but the console outputs the same data in JSON Format as I have added in this question.WHat i want to do is to sort this data at php and do some things at server side – codefreaK Aug 08 '15 at 14:42
  • @Quentin how to make it t JSON Formatted ? – codefreaK Aug 08 '15 at 14:45
  • @SachinDivakar — The console is likely outputting it as a representation of a JavaScript data structure, not a string of JSON. – Quentin Aug 08 '15 at 15:57
  • @SachinDivakar — Why bother making it JSON formatted? The format you are sending already (as seen in the screenshot of the Net tab) will be automatically parsed by PHP and converted into a sensible data structure in `$_POST`. Mixing in JSON just complicates things. – Quentin Aug 08 '15 at 15:58
  • @Quentin so how to access that without json encoding in php am very new to php – codefreaK Aug 08 '15 at 16:16
  • You just get the data straight out of `$_POST`. Use `var_dump` if you want to see what it looks like. – Quentin Aug 08 '15 at 16:17
  • @Quentin can you help me with an example / – codefreaK Aug 08 '15 at 16:20
  • @Quentin I have used var_dump and added its output please check this out – codefreaK Aug 08 '15 at 16:44
  • So what's the problem? You've got a series of nested arrays. – Quentin Aug 08 '15 at 16:47
  • http://codepad.org/jN1frJye this gives unidentified error – codefreaK Aug 08 '15 at 17:32
  • @Quentin thanx for the help figured it out at last just started php programming yesterday evening :P – codefreaK Aug 08 '15 at 18:20

2 Answers2

0

The Data was not json Encoded .It was form encoded as pointed out by @Quentin So instead of using Json Iterator I just used it without encoding to json as normal post parameter

for ($row = 0; $row < sizeof($obj); $row++) {


echo $obj[$row]['place'];

}
codefreaK
  • 3,584
  • 5
  • 34
  • 65
-1

use this code at the start of the file.

<?php
    $postdata = json_decode(file_get_contents("php://input"));
        foreach($postdata as $key => $value){
        $credentials[$key] = $value;
        };
     print_r($credentials) //should return you the php accessible array instead of object.//only for testing purpose


     /*
        You logic to check the database or anyother operation
     */
Alaksandar Jesus Gene
  • 6,523
  • 12
  • 52
  • 83