0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

what if they are 2 fields that need to be updated? one with the latitude, and one with the longitude? thanks again!
1

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');
}

3 Comments

bruno, that's exactly what I want to do... but for some reason the values are not showing up on the fields. I might be doing something wrong. <input name="latitude" id="latitude" type="text" size="47">
If you want to use bruno's code then replace input.longitude with input#longitude and input.latitude with input#latitude - it also might be worth reading this -> api.jquery.com/category/selectors
rATRIJS, thanks so much. Works! Will now read the link you sent. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.