access ajax/jquery returned data and place it to form field
A user enters an address on this form, then click on verify address, and i run this:
$('#verify').click(function () {
var address = $('input[name=address]');
var city = $('input[name=city]');
var state = $('input[name=state]');
var zip = $('input[name=zip]');
var country = $('select[name=country]');
//organize the data for the call
var data = 'address=' + address.val() + '&city=' + city.val() + '&state=' + state.val() + '&zip=' + zip.val() + '&country=' + country.val();
//start the ajax
$.ajax({
url: "process.php",
type: "GET",
data: data,
cache: false,
success: function (html) {
//alert (html);
if (html!='error') {
//show the pin long and lat form
$('.form2').fadeIn('slow');
} else alert('Error: Your location cannot be found');
}
});
//cancel the submit button default behaviours
return false;
});
process.php takes the data, and verifies that the address exists, then sends back the latitude and longitude as "longitude,latitude". How开发者_运维知识库 can I take that data and place it in a form field that shows up if the location is found (form fields in the form2 div). Thanks so much.
Replace this:
$('.form2').fadeIn('slow');
with this:
$('.form2').fadeIn('slow').find('input[name=latlon]').val(html);
where latlon is the name of the input field where you want to insert the latitude and longitude.
Well, I'm litte confused but, if I understand correctly, you can split response by "," separator and simply put each one in respective field. Like this:
if (html!='error') {
var response = html.split(',');
$('.form2').fadeIn('slow');
$('.form2 input.longitude').attr( 'value', response[0] );
$('.form2 input.latitude').attr( 'value', response[1] );
} else {
alert('Error: Your location cannot be found');
}
精彩评论