传递值 - 全部在同一个文件中 - 无法正常工作

时间:2013-02-07 00:32:00

标签: jquery ajax openlayers

我需要你的建议。我是ajax和jquery的新手。我正在使用postgresql 9.1 / postGIS 2.0 / openlayers / geoserver和apache。

我在使用javascript点击地图(openlayers)时获得了功能ID。

使用ajax我试图将此值传递给PHP值并执行查询。 Openlayers,jquery,javascript和ajax工作正常,但php永远不会获得价值。这是我的代码片段。

//this is inside openlayers init function
function selected_feature(event){
//getting the id
var sf = event.feature.fid;
var sfs = sf.split(".");
var sff = sfs[1]
//ajax
$(document).ready(function()
{
 $.post("map.php", { jas: 'sff'});
});

//the php code snippet, after openlayers
$_SESSION['blah'] = $_POST['jas']; 
$blah= $_SESSION['blah'];
echo $blah;
 $queryd='
  SELECT
   pins.p_name 
  FROM
    pins
    WHERE
    pins.p_id='.$blah;
 //fetching to fill array
 $resultd=pg_query($conn, $queryd);
 $nord=pg_num_rows($resultd);
 $d=0;
  while($arrayd=pg_fetch_array($resultd)){
      $name[$d]=$arrayd['p_name'];
    $d++;
        echo $arrayd['p_name'];
                echo $arrayd[$d];

    }

问题是php的回声无效并且查询根本无法正常工作。我知道这个问题听起来像是一个dublicate,但我相信是不同的,因为所有代码都在同一个文件中(名为map.php)。另外,背景上还有一些网络映射。我甚至不知道是否有一个导致问题的网络映射程序。

请告知

谢谢。

EDIT 我刚刚编辑了ajax部分。我用这个替换了

jQuery.post("map.php", { jas : "sff" }, function(data) {
  //  alert(data);
  });

......仍然没有运气

编辑#2

根据this教程我编辑了我的代码。所以现在我在map.php中有这个jquery

jQuery.post("testone.php", { jas : sff }, function(data) {
  alert(data);
  });

在testone.php中这个php代码

<?php
  $jas = $_POST['jas'];
     print $jas;
?>

map.php实际上会警告数据。但是,还有一件事。如何将数据作为PHP值传递回map.php,这样我也可以激活查询?大多数教程都是关于提醒数据或将它们放在<div>中。我想用php。像

这样的东西
   jQuery.post("testone.php", { jas : sff }, function(data) {
    //put returned data in a php var
          });

1 个答案:

答案 0 :(得分:0)

我将你的PHP移动到另一个文件,然后你需要echo json_encode($name);并在你的jQuery帖子的回调处理程序中处理你返回的数据。

$.post('new.php',{jas:'sff'},function(data){ //do something cool with data[0] through the last index of the array });