我想从Leaflet弹出窗口更新PostgreSQL中单元格的值。 我想要做的是在按下表单中的提交按钮时运行javascript函数,然后通过在javascript函数中调用的PHP脚本将已输入的值发送到表单到PostgreSQL数据库。
这是我的弹出功能与其表单相似的内容。
function onEachFeature(feature, layer) {
insertsearch = feature.properties.gid
popUpText = "<b>Name</b>: " + feature.properties.businessna +
"<br> <b>Address: </b>" + feature.properties.addresslin + ", " + feature.properties.addressl_1 +
"<br><b>Food hygiene rating</b> <span id=skala>(1-5)</span> : " + feature.properties.ratingvalu +
"<form id='formoid' action='javascript:insertdata();'><br>Rating (between 1 and 5): <input type='number' name='quantity' min='1' max='5'><input type='submit'></form>"
feature.properties.gid;
feature.properties.popupContent = popUpText;
if (feature.properties && feature.properties.popupContent) {
layer.bindPopup(feature.properties.popupContent);
}
};
这是我想要使用的PHP脚本:( insert.php )
<?php
header("Access-Control-Allow-Origin: *");
$conn_str = "host=localhost dbname=geoweb
user=user password=user";
$dbconn = pg_connect($conn_str)
or die('Could not connect: ' . pg_last_error());
//innumber is supposed to be the rating value given by the user in the HTML form
$innumber=;
//idnumber is supposed to be the GID of the point that is being rated
//GID is this variable in the HTML function (insertsearch = feature.properties.gid)
$idnumber=;
//norate is the NUMBER OF TIMES the place has been rated
$norate= pg_query($dbconn, "SELECT rate FROM rating WHERE gid=$idnumber");
//findnumber is getting THE RATING value that the place already has
$findnumber= pg_query($dbconn, "SELECT rateav FROM rating WHERE gid=$idnumber");
//newvalue adds the existing rating value to the users new rating
$newvalue= $innumber + $findnumber;
//adds on 1 (one) to the norate to update how many times the place has been rated
$newcount= $norate+1;
//the query is updating the new rating value of the place.
//It replaces the old rating value with the a new value
$query="UPDATE rating SET rateav=$newvalue, rate=$newcount WHERE gid=$idnumber";
$result = pg_query($dbconn, $query);
pg_freeresult($result);
pg_close($dbconn);
?>
我的问题是现在弄清楚如何编写javascript函数。据我所知,我应该使用jquery $ post方法通过 insert.php 脚本发送数据。不管怎样,我应该在HTML表单中将参数发送到JS函数(action = ...)?如果是这样,我该怎么做? 我试图通过PHP脚本发送两个值,即弹出函数中的 insertsearch 变量(以获取表中的正确条目),以及用户在表单中给出的评级编号。
感谢您的帮助!
答案 0 :(得分:0)
我使用.ajax
data
选项填充了执行此POST所需的任何参数。确保同时指定URL,dataType和方法以及数据。