Victor Oseghale Victor Oseghale - 3 months ago 10
jQuery Question

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?

Answer

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.