我想从数据库加载数据。 "输出"必须加载,其中包含有关Geofence坐标的一些数据并添加到地图中。这是加载功能:
jQuery("#submitButtonLoad").click(function () {
/*the jQuery.ajax gets openend*/
jQuery.ajax({
/*typ POST gets defined*/
type: "POST",
/*the PHP file that receives the POST*/
url: "parsernew.php",
/*the data that gets transfered*/
success: function(html){ //so, if data is retrieved, store it in html
var vectorOutput = jQuery("#output").val();
var vectorName = jQuery("#vectorName").val();
jQuert('#output').html();
}
}); //close jQuery.ajax(
});
这也是parsernew.php,它从数据库中获取所需的数据。使用config.php完成连接:
<?php
session_start();
print_r($_SESSION);
/*if the id parameter is not set an error occures*/
if (!isset($_SESSION['id'])) {
/*ajax request/abfrage, check.....*/
echo 'the user id is missing';
die();
}
/*if the id parameter is set else gets executed*/
else
{
/*the id of the user gets requested*/
$id=$_SESSION['id'];
/*the parameters get requested and saved into local variables*/
$vector = ($_POST['vector']);
$name = ($_POST['name']);
echo $id;
echo $vector;
echo $geofence;
/*the config.in.php is called*/
/*this programms opens the conncetion to the database*/
include('config.php');
/*the SELECT query gets created*/
$eintrag = "SELECT (geofencename,geometry,fk_user_geofence) VALUES ('$name','$vector','$id') FROM public.geofence";
/*the above mentioned query gets executed
--> this always executes a query, no matter what kind */
$eintragen = pg_query($eintrag);
/*if the query is not correct an error occures*/
if (!$eintrag) {
die("Error in SQL query: " . pg_last_error());
}
}
?>
任何人都可以告诉我问题出在哪里?我需要在地图上加载输出。
此部分已编辑。这是我的保存功能,我想根据它编写加载功能:
/* SAVE BUTTON */
jQuery(document).ready(function(){
/*if the submit Button gets clicked this function is called*/
jQuery("#submitButton").click(function () {
/*the text of the html field output and vectorName are stored into javascript variables
vectorOutput
VectorName*/
var vectorOutput = jQuery("#output").val();
var vectorName = jQuery("#vectorName").val();
/*the jQuery.ajax gets openend*/
jQuery.ajax({
/*typ POST gets defined*/
type: "POST",
/*the PHP file that receives the POST*/
url: "parser22.php",
/*the data that gets transfered*/
data: {
/*vector: contains the geometric information of the overlay, in kml format*/
vector: vectorOutput,
/*name: contains the chosen name of the geofence*/
name: vectorName
},
success: function(html){ //so, if data is retrieved, store it in html
/*if the save button gets clicked the field with the geofencename */
jQuery("#output").val('');
/*and kml koordinates get emptied*/
jQuery("#vectorName").val('');
/*to show the user that it worked alert Window pops up*/
alert("Geofence successfully saved!");
}
}); //close jQuery.ajax(
});
// });
答案 0 :(得分:0)
执行查询的数据库用户是否具有相应的读取权限?另外我假设以下只是一个拼写错误而不是你的正在运行的代码?
jQuert('#output').html();
编辑1:
很难准确理解您要实现的目标。顺便说一句,它更灵活,更喜欢用完成而不是成功做这样的事情:
jQuery.ajax({
// config
}).done(function(data){
// do something with data
jQuery('#output').html(data);
});
编辑2:
在下面的评论中查看示例CRUD链接,加载与保存操作的结构基本上是我从您尝试实现的内容中推断出的结构。 post方法只是一个更具体的方法,所以如果需要,用ajax替换它。尝试尽可能使用您的代码承诺DRY(不要重复自己)原则。在这种情况下,可以在最初使用公共加载方法,然后在调用save方法之后使用。
答案 1 :(得分:0)
success
内的代码毫无意义。即使它会,也会有一个拼写错误: jQuert 。我想你想要的东西:
success: function(html){
var foo = doSomething(html);
jQuery('#output').html(foo);
}