2013-01-16 24 views
0

我需要在單獨的php文件中使用以下座標,我想最好的方法是通過ajax進行傳輸。

的HTML如下:

<div id="canvas"> 
<div class="point1"> 
<div class="coord"></div> 
</div> 

<div class="point2"> 
<div class="coord"></div> 
</div> 

<div class="point3"> 
<div class="coord"></div> 
</div> etc 
</div> 

我目前寫目前jQuery UI的可拖動功能顯示每個頂部和左側位置DIV直接在div下被拖:

... 
element = this; 
var top = $(this).position().top; 
var left = $(this).position().left; 
$('.coord', this).text('Top: ' + top + 'px' + ' Left: ' + left + 'px'); 
... 

但爲了現在在PHP變量中使用每個點座標,是否需要將$('.coordinates', this) etc拆分爲數組,首先通過ajax轉換爲php - 如果有的話,是否有人知道如何?

還是有一個更簡單的方法 - 我敢肯定可能有?!?我只是有點困惑!

欣賞任何建議或指針。

回答

2

好了,我沒有那麼先進,但如果從JavaScript(jQuery的)到PHP,當你使用Ajax說使用jQuery的方法$。員額你想傳遞的數據:

$.post("file.php", { coordTop: top , coordLeft: left }, 
    function(data) {// ON REQUEST DONE YOU CAN RETURN (echo, print,..) DATA 
    alert("Data: " + data); 
    }); 

而在PHP文件你捕獲那些變數$ _POST [「coordTop」]和$ _POST [「coordLeft」]

+0

是啊,我知道ajax post方法本身,我只是不確定是否需要爲每個創建單獨的變量座標或是否完成不同。 – Duncan

+0

現在我明白了,對不起......首先創建一個點的數組,並將其傳遞爲:..「file.php」,{points:arrPoints} ..以後在PHP工作中使用$ _POST [「分」] ...我會看到它,如果我發佈代碼。 – paido

+0

好吧,我擁有它。正如我所看到的你的HTML,我會在每個座標中傳遞一個JSON: 'JSON.stringify(arrPoints)'其中arrPoints是:'arrPoints = {}; $(「。coord」)。each(function(index,element){arrPoints [$(this).parent()。attr(「class」)] = $(this).text();}' 及更高版本在PHP中使用 '$ arrPoints = json_decode($ _ POST [「points」]);' – paido