1

I have a PHP script that sends this array to the client via ajax:

Array
(
    [0] => $209.90
    [1] => $20.99
    [2] => $188.91
)

I am sending the array to the client this way:

return $servicePrices;

I have this jQuery statement:

success: function(data) {
  //console.log('Success'+data);
  $( '#tourSubtotal' ).val( data[0] );
  $( '#tourDiscount' ).val( data[1] );
  $( '#tourTotal' ).val( data[2] );
},

My results for each ID value are A, r and r. Instead, how can I get the 3 currency data values from the array in my ajax jQuery?

4
  • 2
    Did you parse your PHP array into a JSON object or something like that? Commented Jun 14, 2018 at 10:30
  • 1
    How are you sending these to the client? Commented Jun 14, 2018 at 10:31
  • See my amended OQ @MasivuyeCokile on how I send the array from PHP to jQuery -- thanks. Commented Jun 14, 2018 at 10:34
  • @H.Ferrence Based on my answer, would you like to post the full code of PHP and I can help you design your end-point to make changes. Or you wanna take it from my answer and give me a shout if you are stuck or something. Commented Jun 14, 2018 at 10:36

2 Answers 2

7

It looks like you are using print_r() function to display this:

Array
(
    [0] => $209.90
    [1] => $20.99
    [2] => $188.91
)

In that format it's just as useful as any string is. You have to use json_encode() to convert it to a format both languages understand like:

["$209.90", "$20.99", "$188.91"]

Your PHP code should be something like:

<?php
  $json = some_value;
  // Remove this:
  // print_r($json);
  // Instead, write this:
  echo json_encode($json);
?>

And then you can use it in JavaScript, the way you have specified. Also, jQuery is smart enough to understand that the response is JSON and you don't need JSON.parse() in most of the cases.

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

3 Comments

I've added the "string" part to try to clearify that like that it's not an array, but a string
"jQuery is smart enough to understand that the response is JSON" - if the server is well-behaved and sets correct Content-Type header, or the client forces dataType or uses $.getJSON. If no such hints are present, jQuery should opt for safety and do no extra processing.
@Amadan Yep, agreed. :D
3

You can return the array in a JSON encoded format from PHP as mentioned below:

$result = array(
    '0' => '$209.90'
    '1' => '$20.99'
    '2' => '$188.91'
);
return json_encode($result);

And on the AJAX success function you can use :

success: function(data) {
   var temp = JSON.parse(data);
   $( '#tourSubtotal' ).val( temp[0] );
   $( '#tourDiscount' ).val( temp[1] );
   $( '#tourTotal' ).val( temp[2] );
},

Hope this helps.

2 Comments

With the right MIME type and/or jQuery $.ajax option dataType, you don't actually need JSON.parse, jQuery does it for you.
Worked perfectly @Kishen Nagaraju !! Thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.