私が作成しているものの短くて甘い説明は、ユーザーの住所をジオコーディングし(送信をクリックすると)、取得した緯度と経度の値を非表示フィールドに入れる連絡先フォームです。
すべてが機能していますが、2 つの別々のボタンがあり、必要なのは 1 つだけです。したがって、現時点では、ユーザーは送信ボタンをクリックする前にまずボタンを 1 つクリックする必要がありますが、これはばかげています。[ジオポイントの検索] ボタンは js 関数を呼び出し、緯度と経度の値をフォームの非表示フィールドに返します。その後、送信ボタンを押すと、すべてのフォーム データが MySQL データベースにポストされます。
繰り返しますが、私のコードではすべてが機能しています。フォーム内の両方のボタンを 1 つにするだけで、引き続き機能します。
これはJavascriptです:
<script type="text/javascript">
var geocoder;
function initialize() {
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById("map_canvas"));
}
function updateCoordinates(latlng)
{
if(latlng)
{
document.getElementById('lat').value = latlng.lat();
document.getElementById('lng').value = latlng.lng();
}
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
updateCoordinates(results[0].geometry.location);
} else {
alert("The address you entered could not be found: " + status);
}
});
}
</script>
これは次の形式です。
<form align="center" method="post">
<label for="firstName">First Name:</label> <input type="text" name="firstName"/><br/>
<label for="lastName">Last Name:</label> <input type="text" name="lastName"/><br/>
<label for="address1">Address 1:</label> <input type="text" id="address" name="address1"/><br/>
<label for="address2">Address 2:</label> <input type="text" name="address2"/><br/>
<label for="city">City:</label> <input type="text" id="city" name="city"/><br/>
<label for="state">State:</label><select id="state" name="state">
<option value="CA"<?php echo(isset($_POST['state'])&&($_POST['state']=='CA')?' selected="selected"':'');?>>CA</option>
</select><br/>
<label for="zip">ZIP Code:</label><input type="text" id="zip" name="zip">
<br/>
<label for="location">Location:</label><select name="location">
<option value="Curb"<?php echo(isset($_POST['location'])&&($_POST['location']=='Curb')?' selected="selected"':'');?>>Curb</option>
<option value="Garage"<?php echo(isset($_POST['location'])&&($_POST['location']=='Garage')?' selected="selected"':'');?>>Garage</option>
<option value="Driveway"<?php echo(isset($_POST['location'])&&($_POST['location']=='Driveway')?' selected="selected"':'');?>>Driveway</option>
<option value="FrontDoor"<?php echo(isset($_POST['location'])&&($_POST['location']=='FrontDoor')?' selected="selected"':'');?>>Front Door</option>
<option value="SideDoor"<?php echo(isset($_POST['location'])&&($_POST['location']=='SideDoor')?' selected="selected"':'');?>>Side Door</option>
<option value="Inside"<?php echo(isset($_POST['location'])&&($_POST['location']=='Inside')?' selected="selected"':'');?>>Inside</option></select><br/>
<input type="hidden" id="lat" name="lat" value="" /><br>
<input type="hidden" id="lng" name="lng" value="" /><br>
<input type="button" value="Find Geopoints" onclick="codeAddress()"/>
<input name="submit" type="submit" id="submit" value="Submit"/>
</form>
これは、MySQL にデータを送信するための PHP です。
<?php
if(isset($_POST['submit']))
{
$con = mysql_connect("localhost","x","y");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("x", $con);
$sqlCmd = sprintf("INSERT INTO x (firstName, lastName, address1, address2, city, state, zip, location, lat, lng) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
mysql_real_escape_string($_POST["firstName"]),
mysql_real_escape_string($_POST["lastName"]),
mysql_real_escape_string($_POST["address1"]),
mysql_real_escape_string($_POST["address2"]),
mysql_real_escape_string($_POST["city"]),
mysql_real_escape_string($_POST["state"]),
mysql_real_escape_string($_POST["zip"]),
mysql_real_escape_string($_POST["location"]),
mysql_real_escape_string($_POST["lat"]),
mysql_real_escape_string($_POST["lng"]));
//echo $sqlCmd;
//die();
mysql_query($sqlCmd);
mysql_close($con);
}
?>