I tried to use the callback function but it still has no effect. Here is the HTML:
<form action="{% url 'seller_details' %}" method="post" id="seller_details">
{% csrf_token %}
<div>
<div class="heading">PERSONAL INFORMATION</div>
<table>
<tr>
<td id="wd25">Full Name</td>
<td><input type="text" placeholder="Full Name" name="full_name" value="{{full_name}}"/></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type="text" placeholder="Mobile Number" value="{{mobile}}" name="mobile"/></td>
</tr>
<tr>
<td>Store Address</td>
<td><textarea rows="5" placeholder="Store Address" name="pickup_address" value="{{pickup_address}}"></textarea></td>
</tr>
</table>
</form>
Then I tried to validate the form and submit the form using submit handler of jQuery validate
$("#seller_details").validate({
rules: {
full_name: "required",
pickup_address:"required",
},
messages: {
full_name: "Please enter Name",
mobile: "Please enter Mobile Number",
pickup_address:"Please enter address",
},
submitHandler: function(form){
var form = $(form).serialize()
codeLatLng(storeGeoAddress);
console.log(latitude)
console.log(longitude)
$.ajax({
type: "post",
url: "{% url 'seller_details' %}",
data: form + "&csrfmiddlewaretoken=" + '{{csrf_token}}' + '&latitude=' + latitude + '&longitude=' + longitude,
I used the following asynchronous function called geocoder to get the latitude and longitude of the address submitted
function codeLatLng(callback) {
var address = $("textarea[name=pickup_address]").val();
if (geocoder) {
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
callback(results[0]);
} else {
alert("No results found");
}
}
else {
alert("Geocoder failed due to: " + status);
}
});
}
}
function storeGeoAddress(addr) {
latitude = addr.geometry.location.lat();
longitude = addr.geometry.location.lng();
}
The problem I am facing here is that I am not able to gather the values of latitude and longitude so that I can give it back to my submit handler so that it can post the values? What can I do in such a scenario? Thank you
Latitude and Longitude are globally defined here
console.log(latitude) console.log(longitude) are not able to log anything
Is there any Way by Which i can make Submit Handler as my callback function?
latitudeandlongitudevariables? If they're within scope of both functions then what you have should work, although it's rather odd placing a JSON string in the querystring of a request. You would be best just sending a plain object and letting jQuery encode it for you.latitude and longitude.$.ajaxwill run before thestoreGeoAddresscallback gets fired because the logic instoreGeoAddressis async. You need to perform your AJAX in your callback. Then you can also sack of the nasty global variables.