doubianyan9749 2015-08-21 05:38
浏览 45

地理编码无法处理php mysql提交表单

What I want to happen: User inputs customer data (name,address, etc) on a form, hits the submit button and that users info is updated to a msql database table. There is also a map page that calls on the mysql database and updates the map (not included in the code because that page works fine).

The issue: Gecode on this page isn't working. I want to have the lat/lng geocoded and saved to the table at the same time as the rest of the information so that if they go to the map screen next, there will be an updated marker at the address saved on this page. As it stands right now the lat and lng fields always show 0.000000, without any errors being reported. What am I doing wrong?

My code (saved as index.php):

<!DOCTYPE html>

<HTML>
<HEAD>
<TITLE>
</TITLE>

<style>
body {
background-color: #f2f2f2;
}

table {
border-spacing: 0.5rem;
border-collapse: collapse;
}

td {
border: 1px solid #000000;
padding: 0.2rem;
}
::-webkit-input-placeholder {
text-align: center;
}

:-moz-placeholder {
/* Firefox 18- */
text-align: center;

}

::-moz-placeholder {
/* Firefox 19+ */
text-align: center;

}

:-ms-input-placeholder {

text-align: center;

}
.a {
background-color: #ffffff;
box-shadow: 10px 10px 5px #888888;
border-radius: 25px;
border: 1px solid #000000;
padding: 10px;

width: 80%;

}
.b {
background-color: #ffffff;
box-shadow: 10px 10px 5px #888888;
border-radius: 25px;
border: 1px solid #000000;
padding: 10px;

width: 80%;

}
img {
box-shadow: 10px 10px 5px #888888;
border-radius: 25px;
}
.h  {
width: 80%;

margin-left: auto;

margin-right: auto;
height: 10px;
background: url(hr.png) no-repeat center;
border: none;
}

</style>
<link rel="icon" 
type="image/png" 
href="Icon2.png" />
</HEAD>
<BODY>
<center>
<img class"i" src="companyname.png">
<p>
<h2>
Customer Control Panel 
</h2>


<div class="a" id="add_customer">
<form id="customerdata" name="customerdata" method="post" onsubmit="geoCode()">
<input type="text" align="center" id="name" name="name" placeholder="Customer Name">
<input type="text" align="center" id="address" name="address" placeholder="Address">
&nbsp&nbsp&nbsp
<b>
Paid?:
</b>
&nbsp
<select id="type" name="type">
<option value="select">
Make a Selection
</option>
<option value="Yes">
Yes
</option>
<option value="No">
No
</option>
<option value="Not">
Not a customer
</option>
</select>
<input type="text" align="center" id="comments" name="comments" placeholder="Comments. Limited to 150ch">
<input type="hidden" name="lat" id="lat" value="" />
<input type="hidden" name="lng" id="lng" value="" />
<input type="submit" id="submit" name="submit" value="Add Customer">
</form>
</div>

<!-- Start of PHP FORM SUBMITION -->

<?php
if (isset($_POST['submit'])) {
sleep(3);
include 'db.php';
$con = mysql_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_db", $con);         
$name = $_POST['name'];
$address= $_POST['address'];
$lat= $_POST['lat'];
$lng= $_POST['lng'];
$type= $_POST['type'];
$comments= $_POST['comments'];
$sql= "INSERT INTO `mysql_table`(name,address,lat,lng,type,comments) VALUES ('$name','$address','$lat','$lng','$type','$comments')";
$a=mysql_query($sql);
echo "<br/><br/>";
if (!$a)

die("Error adding record: " . mysql_error());

else

echo "<br>"."1 record added";
}
?>
<!-- End of PHP FORM SUBMITION -->



<!-- Start of Customer viewing Table -->
<br>
<hr class="h">
<p>

<br>
<br>

<div class="b">
<table bgcolor="#FFFFFF">
<tr align="center">
<td>
<b>
Name
</b>
</td>
<td>
<b>
Address
</b>
</td>
<td>
<b>
Paid?
</b>
</td>
<td>
<b>
Comments
</b>
</td>
</tr>
<?php
include 'db.php';
$con = mysql_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_db);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("mysql_db", $con);

$query = mysql_query("SELECT `name`, `address`,`lat`,`lng`,`type`,`comments` FROM `my_table` WHERE 1");
while($row = mysql_fetch_array($query, MYSQL_ASSOC))
{

$name = $row['name'];
$address = $row['address'];
$lat = $row['lat'];
$lng = $row['lng'];
$type = $row['type'];
$comments = $row['comments'];
$name = htmlspecialchars($row['name'],ENT_QUOTES);
$comments = htmlspecialchars($row['comments'],ENT_QUOTES);

echo "<tr align='left'><td>$name</td><td>$address</td><td>$type</td><td>$comments</td></tr>";
}
mysql_close($db);
?>
</table>
<!-- End of Customer viewing Table -->

</div>
</center>

<script>
var geocoder = new google.maps.Geocoder();

document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}

function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>

</BODY>
</HTML>

EDITED 8/21/15

I updated the code section to reflect what I have found on the Google Maps JavaScript API. Originally I had the geocode script in the <head> but according to the google example it goes in the body. I was able to get the example code to work:

<!DOCTYPE html>
<html>
<head>
<title>Geocoding service</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 80%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
#panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
}


#panel, .panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}

#panel select, #panel input, .panel select, .panel input {
font-size: 15px;
}

#panel select, .panel select {
width: 100%;
}

#panel i, .panel i {
font-size: 12px;
}

</style>
</head>
<body>
<div id="panel">
<input id="address" type="textbox" value="">
<input id="submit" type="button" value="Geocode">
</div>
<div id="map"></div>
<div id="lat"></div>
<div id="lng"></div>
<p>


<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {lat: 32.2209784, lng: -110.8833395}
});
var geocoder = new google.maps.Geocoder();

document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}

function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
document.getElementById("lat").innerHTML = results[0].geometry.location.lat();
document.getElementById("lng").innerHTML = results[0].geometry.location.lng();
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}

</script>
<script src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"
async defer></script>
</body>
</html>

But it's calling the address from within the map, I want to call the address from the form, which I thought wouldn't really matter as long as the names and id's were correct but I still can't seem to get my code to work right. With the sample code I was able to write the lat and lng to separate <div> tags so I know that the geocode is working. Could it be that my file is saved as a .php?

  • 写回答

1条回答 默认 最新

  • duanhanzi8328 2015-08-21 06:31
    关注

    The values of your INSERT statement doesn't match to the fields. There are 4 fields but you are passing 6 values.

    Please change

    $sql= "INSERT INTO `my_table`(name,address,type,comments) VALUES ('$name','$address','$lat','$lng','$type','$comments')";
    

    to

    $sql= "INSERT INTO `my_table`(name,address,type,comments) VALUES ('$name','$address','$type','$comments')";
    

    OR you have to add the fields for the coordinates to your field list.

    --

    It looks like you are not using the variables lat and lng in your geoCode function. You should add two hidden inputs to your HTML form by adding

    <input type="hidden" name="lat" id="lat" value="" />
    <input type="hidden" name="lng" id="lng" value="" />
    

    and using

    document.getElementById('lat').value = results[0].geometry.location.lat();
    document.getElementById('lng').value = results[0].geometry.location.lng();
    

    in your geoCode function.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大