I am creating a mobile app which relies on jSON to submit forms to a database. I have my submit function:
function sendJson(service, method, json) {
var request = $.ajax ({
type: 'POST',
url: '/remoteserver/service/' + service + '/' + method,
dataType: 'json',
async: false,
data: JSON.stringify(json),
success: function (msg) {
alert('Success ' + JSON.stringify(msg.location));
},
error: function(msg) {
alert('YOU SUCK' + JSON.stringify(msg));
}
});
}
and currently am using something like this to populate the jSON string:
$("element").click(function(){
var wrapper = {};
var location = {};
wrapper.location = location;
location.name = $('#name').val();
location.address1 = $('#address1').val();
location.address2 = $('#address2').val();
location.city = $('#city').val();
location.state = $('#state').val();
location.country = $('#country').val();
location.zipCode = $('#zipCode').val();
location.contactName = $('#contactName').val();
location.contactPhone = $('#contactPhone').val();
sendJson("locationService", "createLocation", wrapper);
});
My question is this - I will have somewhere near 100 forms in this app. How do I go about getting each jSON element(? - IE location.name) to map to the form field w/o having to explicitly state location.name = $('#name).val();
or is this how it's done in jSON? I have searched extensively and everything seems not suited to what Im trying to do. thank you.