Hi I have an address field taking address from france only. I want that address to be stored with longitude and latitude in my database.. Is there a way to do that.. I'm trying several codes but none of them are working.
here is my code
<form id="contact" action="send.php" method="post"><br>
<h2>Formulaire de contact</h2>
<div id="locationField" required>
<input id="autocomplete" placeholder="Adresse"
onFocus="geolocate()" type="text" name="address" required="required"></input>
</div><br>
<input type="hidden" name="latitude" id="at" placeholder="Latitude" class="form-control" required="required"/>
<input type="hidden" name="longitude" id="lng" placeholder="Longitude" class="form-control" required="required"/>
<input type="submit" name="submit" value="submit" class="button"/>
<script>
function initAutocomplete() {
autocomplete = new
google.maps.places.Autocomplete(
/** @type {!HTMLInputElement}
*/(document.getElementById('autocomplete')),
{types: ['geocode'], componentRestrictions: {country: 'fr'}});
autocomplete.addListener('place_changed', fillInAddress);
}
document.getElementById('latitude').value = place.geometry.location.lat();
document.getElementById('longitude').value = place.geometry.location.lng();
</script>
<script src="https://maps.googleapis.com/maps/api/js?input=Paris&types=geocode&key=xxxxxxxxxxxxxxxxxxxxxx&libraries=places&callback=initAutocomplete"
async defer></script>
</form>
<?php
if (isset($_POST["submit"])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "web_contact";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO customer1(address,latitude,longitude)
values('" . $_POST["address"] . "','" . $_POST["latitude"] . "','" . $_POST["longitude"] . "')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "" . mysqli_error($conn);
}
}