2013-01-21 113 views
0

可能重複:
How to pass JavaScript variables to PHP?
Passing a Javascript string to PHP返回JavaScript函數PHP變量

我有這個JavaScript的PHP文件中的代碼:

<script> 
function(){ 
var mail="[email protected]"; 
for(var i=0; i<json_load.length; i++){ 
    if(json_load[i].custom.tipologo == "FAAC FILIALI"){ 
     mail=json_load[i].custom.email; 
    } 
} 
return mail; 
} 
</script> 

我需要將「郵件」變量返回給PH P變量。我怎樣才能做到這一點??

+0

郵政/ get是trasnmitting數據的標準方法通過http – DavidB

+0

這麼多重複的問題,這是很難知道哪些鏈接到。您需要了解Ajax,以及更一般地瞭解客戶端/服務器環境(如瀏覽器/ Web服務器)中的prorgams如何工作。 – SDC

回答

2

要麼通過jQuery中的submit()提交郵件變量,要麼使用AJAX

手冊:AJAXsubmit

$.ajax({ 
    type: "POST", 
    url: window.location.href, 
    dataType: "json", 
    data: mail,  
    success: function(response) { 
     //do something with response  
    } 
}); 
0

如果你瞭解的基礎知識,你不能傳遞的Java腳本值到PHP變量。因爲PHP在服務器端呈現,並且java腳本在客戶端呈現。 Read this

你可以做的是,你可以將數據傳遞到使用AJAX服務器,

$.ajax({ 
    type: "POST", 
    url: path/to/the/php/file.php, 
    mail: mail,  
    success: function(response) { 
    //if you return something from the server-side you get it here  
    } 
});