2013-02-07 23 views
1

我需要您的建議。我是新來的ajax和jQuery。我使用postgresql 9.1/postGIS 2.0/openlayers/geoserver和apache。傳遞值 - 全部在同一個文件中 - 不起作用

我管理得到一個功能的ID時點擊地圖(openlayers),使用JavaScript。

使用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)中。另外,背景上還有一些網絡映射。我甚至不知道是否導致問題的網絡映射程序之一。

請告知

謝謝。

編輯 我剛剛編輯了ajax部分。我把它換成這個

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

...仍然沒有運氣

編輯#2

根據this教程中,我編輯我的代碼。所以現在我有這個jQuery在我map.php

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 
      }); 

回答

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 });

+0

@ Jarealist首先,謝謝你的安塞爾。我只是編輯了我的問題......你的意思是執行查詢並將結果呈現在另一個文件中?但我需要將它們全部放在同一個文件中......無論如何,如果兩個不同的文件是唯一的方法,那麼include_once(second-file.php)'如何發出?所以用戶不知道什麼,不必去另一個文件來查看結果。 – slevin

+0

這就是AJAX的美麗。用戶不會被路由到另一個頁面。只有工作會在另一個頁面上完成,並通過AJAX返回到頁面。 您的PHP代碼需要通過json_encode函數來回顯數組,如 'while($ arrayd = pg_fetch_array($ resultd)){ $ name [$ d] = $ arrayd ['p_name']; $ d ++; } echo json_encode($ name); '。 – Jarealist

+0

好吧,我認爲我有哲學,但不是技術部分。你能給我一個鏈接或教程什麼的?指導方針?所以我可以研究它? – slevin

相關問題