0

Here is my code to call AJAX and get the response from another PHP file:

$.post('<?php echo get_site_url(); ?>/ajax-script/',{pickup:pickup,dropoff:dropoff,km:km},
        function(data){
            $('#fare').html(data);
            $('#loading_spinner').hide();

        });

ajaxscript.php file

$jsonData = '{"fare":30580,"actual_distance":1519,"city":"Islamabad","status":true}';

$json = json_decode($jsonData,true);
echo $json['fare'];

This code gives me the fare at the time of $('#fare').html(data);

But I need to extract the city from JSON, too, and for this I added an extra line in ajaxscript.php:

echo $json['city'];

After doing this, it gives me 30580Islamabad

How can I store these two values separately in JavaScript? I need them for future work.

5
  • You are doing everything backwards Commented Aug 18, 2016 at 9:39
  • i dont understand what you exactly meant ? Commented Aug 18, 2016 at 9:40
  • Pass whole json as a result. Commented Aug 18, 2016 at 9:40
  • Simply echo $json; Commented Aug 18, 2016 at 9:41
  • its now show the "Array" in output, now how can i store $json['city'] values in variable after it gets the response ? Commented Aug 18, 2016 at 9:43

2 Answers 2

2

You are doing everything backwards

Your PHP should be

$jsonData = '{"fare":30580,"actual_distance":1519,"city":"Islamabad","status":true}';

//$json = json_decode($jsonData,true);
echo $jsonData;

As you already have a JSONString to send to your javascript.

Then your javascript will recieve a javascript object in the data parameter of

$.post( '<?php echo get_site_url(); ?>/ajax-script/', 
          {pickup:pickup,dropoff:dropoff,km:km}, 
   function( data ) {
        $('#fare').html(data.fare);
        $('#city').html(data.city);
        $('#loading_spinner').hide();
}, "json");

Note the "JSON" at the end of the javascript to tell it to expect a JSON Object, it will then convert the JSONString to a javascript Object automatically for you so the data parameter will be an onbect

Sign up to request clarification or add additional context in comments.

4 Comments

well that $jsonData is getting response from a web service, after using your code it gives me nothing in the output
exactly, you used array, but I am using web service and it gets me the response and I saved that response in $jsonData
Ah I see. So you can just pass that whole string as the echoed data. That already is a JSONString and needs no amendment
I amended the javascript to fits better with the way you were calling post in your code
-1
Add Special characters at the end of each value and in jquery, using jquery split, cut the variable and display

like below;

$jsonData = '{"fare":30580^^,"actual_distance":1519^^,"city":"Islamabad^^","status":true}';

$json = json_decode($jsonData,true);
echo $json['fare'];


in jquery

function(data){
var tdata = data.split("^^");
            $('#fare').html(tdata[0]);
            $('#loading_spinner').hide();

        });

4 Comments

$jsonData coming from a web service, I cant change the response
did the service getting the key value pairs or not?
i have added the response from service in my code $jsonData in ajaxscript file
that has got to be the worst idea since ashtrays on motor cycles

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.