0
votes

I've got everything going great up to a certain point. I have a form where users fill out their address in form (separate fields for street, city, state, zipcode). Once they hit submit, on the submit button, I have a series of onClick functions. It consolidates their address fields into one hidden field seperated by commas and spaces (like you would handwrite your address). This works fine. Then once it's consolidated, the next function geocodes the address and returns the latitude and longitude to hidden fields on the form as well. This works too (I think) here is the code where I do that.

function codeAddress1() {

      var sAddress = document.form.post_consolidatedAddress1.value;
      var storedLatLng;
      var lon;
      var lat;
      var splittingPoint;
      var stoppingPoint;


      geocoder.geocode( { 'address': sAddress}, function(results, status) {

          if (status == google.maps.GeocoderStatus.OK) {

                  document.form.longitude1.value = results[0].geometry.location.lng();
                  document.form.latitude1.value = results[0].geometry.location.lat();
                    alert(document.form.latitude1.value);
                    alert(document.form.longitude1.value);


          } else {

              alert("Geocode was not successful. Please contact the site webmaster and inform them of the following error: " + status);
          }
        });

  }

At least when it shows me the alerts, it has latitude and longitude in them. Here's my problem arises though: Once I have those values in the hidden field, I need to submit it to a MySQL database to be used for sorting by distance later. I have structured the database so that the the latitude and longitude fields are type FLOAT with lengths of 10,6 (large enough to contain the returned values I've gotten through testing). But when I submit to the database, I never actually get the value, I only get 0.000000. I've tried several methods of making the value posted from the form a float, this currently what I have in my PHP where I get the values to submit:

$latitude1 = floatval($_REQUEST['latitude1']) ;
$longitude1 = floatval($_REQUEST['longitude1']) ;

I've also tried:

$latitude1 = (float) $_REQUEST['latitude1'] ;
$longitude1 = (float) $_REQUEST['longitude1'] ;

Nothing ever works... Can somebody please help?

1
You'll have a higher chance of getting answers if you go back and accept answers to your other questions first. - Matt
I have. Sorry, on my other questions, the reason no answer was selected was either that I answered it myself (and didn't select my own answer as the official answer) or that the answer was in the comments, not an actual answer. But I've gone back through and selected as best I could. I do still really need help here, I can't find a solution on my own and I have been working on it for days now. - Justgrant2009
Are you sure you even need to use floatval or cast it as a float? Have you tried just inserting the values? Also, after using floatval or casting as float, do the variables $latitude1 and $longitude1 contain the proper value or do they contain 0.000000? Do a little debugging to see where the lat/lon values are actually lost and report back. You probably don't have answers b/c the issue isn't necessarily with your Javascript, it's probably in your PHP, but you've posted very little of it here. - Matt
Your alerts are client-side. Ensure in your PHP that the variables contain the values you expect before you do anything to them. Don't assume it's working correctly. - Matt

1 Answers

0
votes

Ok, I've got it working. What I've done to get it working is: I now run the consolidation, and geocoding scripts on a separate click. I added a checkbox that asks the user to confirm that all information entered is correct, and the onclick for that checkbox runs the scripts to set the address consolidation, and set the latitude and longitude. Then they click a separate button to submit the form. Then on the php form, I don't bother adjusting the data type, I just let it go to the database as-is and let the database handle it as a float. Then I just have the form validate that the checkbox has been selected before submitting the form. This has worked and the values show up in the database correctly. Finally.