2013-03-20 43 views
1

JavaScript變量這是我的代碼: 在一個函數:如何訪問在PHP

var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day; 
    document.form1.bookingsession.focus(); 
var coords = { 
    "lat": daaa 
}; 
$.get('bs.php', coords, function() { 
    alert('data sent'); 
}); 

和我的PHP代碼:

<?php 
$output = isset($_GET['lat']) ? $_GET['lat'] : 0; 
print("$output"); 
?> 

當我打印$輸出我得到的值爲0,但實際上必須得到變量daaa上的值。讓我知道我犯的錯誤.... 預先感謝您

+0

將其分解爲最簡單的請求。打開開發人員工具控制檯:'$ .get('bs.php',{lat:50});' - 請求返回什麼? – 2013-03-20 10:27:23

+0

即使將我的編碼替換爲$ .get('bs.php',{lat:50}),它仍會返回值0。 – Ganesh 2013-03-20 11:04:03

回答

0

改變這種

var coords = { 
"lat": daaa 
}; 

var coords = { 
lat: daaa 
}; 

你弄丟了雙引號。從jQuery的

http://api.jquery.com/jQuery.get/

例如

//$.get("test.php", { name: "John", time: "2pm" }); 
+0

我替換引號,但仍然得到相同的錯誤,感謝您的幫助先生... – Ganesh 2013-03-20 10:22:48

+0

@Ganesh你如何訪問'bs.php'?意味着直接在瀏覽器中打開'bs.php'。 – 2013-03-20 10:26:43

+0

S先生通過在瀏覽器中直接打開它來檢查其值... – Ganesh 2013-03-20 10:41:29

0

試圖改變

var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day; 

var daaa=document.getElementById('da').value + "-" + year+ "-" +month+ "-" +day; 

反正在這種情況下,你的 「一把手」 動作笑如果變量是真的在客戶端設置的話。你可以通過alert(daaa)或console.log(daaa)來完成;

+0

我想發送變量的值daaa到服務器端執行一些驗證... – Ganesh 2013-03-20 10:50:04

0

首先要做提到的動作@coramba,

然後修改你的JavaScript代碼:

$.get('bs.php', coords, function (dataThatPhpPrints) { 
    alert(dataThatPhpPrints); 
}); 

..你會看到,它實際上返回您發送到PHP值腳本和PHP腳本返回的打印。

爲了保證自己,你的腳本打印一些修改PHP代碼如下喜歡:

<?php 
$output = isset($_GET['lat']) ? $_GET['lat'] : 0; 
print("$output - is response from the PHP!!!"); 
?> 

現在,當你運行侑JavaScript中,你會得到一個警告消息像

ActualValueOfLATYouSentToPHP - is response from the PHP!!! 

熊記住,如果你想測試你的PHP是否有效,你只能在瀏覽器中打開它並等待一些奇蹟發生,因爲它使用$ _GET來獲得一些值,這意味着你需要將它提供爲一個QueryString關鍵是'lat':

yourhost/bs.php?lat=someTestValue 
+0

我得到的警報框中的daaa的值此外我收到一些腳本,我返回的驗證也顯示在警報中,爲什麼該腳本是返回該警報?謝謝你的幫助先生... – Ganesh 2013-03-21 05:11:30