Victor Oseghale
Victor Oseghale

Reputation: 5

Ajax return formatted integers

I have a SQL statement to retrieve int values formatted with 2 decimals:

SELECT FORMAT(`budget`.`transportation`,2), FORMAT(`budget`.`accomodation`,2) 
FROM `budget`

My json_encode returns:

{
 "FORMAT(`budget`.`transportation`,2)":"343.00",
 "FORMAT(`budget`.`accomodation`,2)":"343.00"
}

I want to display it in my form $("#transportation") and $("#accomodation")

Here is a portion of my Ajax to handle it:

var budget       = $("#modal-budget"), 
    activityName = $("#modal-activityName"); 

$.ajax({
        type: "POST",
        url: "Ajax.php",
        data: {
                // Data to server
              },
        success : function(data) {
            // Parse result as JSON
            var res = JSON.parse(data);
...
// Update modal fields
transportation . text(res.transportation);
accomodation . text(res.accomodation);

The issue is with the values returned by Ajax:

{
 "FORMAT(`budget`.`transportation`,2)":"343.00"
 ...
}
// Instead of: 
{
 "transportation":"343.00", 
 "accomodation":"343.00"
}

Is there a way to solve this or make Ajax return currency format for each field?

Upvotes: 0

Views: 121

Answers (1)

jogesh_pi
jogesh_pi

Reputation: 9782

USE AS to set alias name

SELECT FORMAT(budget.transportation,2) AS transportation, 
       FORMAT(budget.accomodation,2) AS accomodation 
FROM budget

Then your returning json data should be like this:

{"transportation":"343.00", "accomodation":"343.00"}

Hope this will help you.

Upvotes: 1

Related Questions