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}]
share|improve this question
    
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 7 '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 – Sachin Divakar Aug 7 '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 8 '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 – Sachin Divakar Aug 8 '15 at 14:42
    
@Quentin how to make it t JSON Formatted ? – Sachin Divakar Aug 8 '15 at 14:45
up vote 0 down vote accepted

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'];

}
share|improve this answer

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
     */
share|improve this answer
    
invalid argument for for each – Sachin Divakar Aug 8 '15 at 12:53
    
That is how you parse a JSON formatted request. We can see the code used to make the request: It isn't JSON formatted. – Quentin Aug 8 '15 at 14:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.