Victor Oseghale Victor Oseghale - 1 year ago 95
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`



I want to display it in my form

Here is a portion of my Ajax to handle it:

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

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:

// Instead of:

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download