I have a field within a form for users to enter an address. After they enter the address, it's sent with jQuery.ajax to a remote API to be verified and parsed to individual fields. The result is a jsonp object that I manipulate to extract the fields I need.
My goal is to include the address with its multiple parts so that it can be processed along with the rest of the fields in the form. The only way I could come up with so far was to add hidden fields populated with the correct values. This feels sort of clunky though, so I was wondering if there is a better way of going about it.
Currently, the user enters the address in a single line, similar to this:
<form method="POST" id="myForm">
<input type="text" id ="address>
<div class="recipients"></div>
<input type="submit">
</form>
The ajax is fired when they focusout of the address field, and on the successful return of the jsonp object the necessary fields are extracted, then inserted into the form:
success: function(data) {
var results = data.results[0];
var address1 = results.Address1;
var address2 = results.Address2;
var city = results.Locality;
var state = results.AdministrativeArea;
var zip = results.PostalCode;
var addLines = "<input type='hidden' name='address1' value='"+address1+"'>";
addLines += "<input type='hidden' name='address2' value='"+address2+"'>";
addLines += "<input type='hidden' name='state' value='"+state+"'>";
addLines += "<input type='hidden' name='city' value='"+city+"'>";
addLines += "<input type='hidden' name='zip' value='"+zip+"'>";
$(".recipients").append($(addLines));
}
Is there a more elegant way of including the returned data into POST? Thanks for the help!