2017-02-02 51 views
0

我有兩頁。在第一頁,名爲test1.html,我嘗試檢索用戶時區。我想將它發送到名爲test2.php的php頁面,並使用變量(timezone)加載該頁面而不是test1。這是代碼。 Test1.html:通過jquery傳遞變量,打開php頁面並提取它

<script type="text/javascript"> 

$(document).ready(function() { 
    var tz = jstz.determine(); // Determines the time zone of the browser client 
    var timezone = tz.name(); //'Asia/Kolhata' for Indian Time. 

$.ajax({ 
     type: 'POST', 
     url: 'test2.php', 
     data: {'timezone': timezone}, 
     cache: false, 
     success: function(){ 
       setTimeout(function() { 
         window.location = 'test2.php'; 
       }, 3000);//this will redirct to somefile.php after 3 seconds 
     } 
    }); 
}); 
</script> 

Test2.php

<?php 

if(isset($_POST['timezone'])) 
{ 

     $tz = $_POST['timezone']; 
     echo "Timezone is " .$tz; 
} 
else { 
     echo "Fail!"; 
} 

?> 

在test2.php的頁面加載,我只得到 '失敗!'信息。 jquery和php部分的工作是正確的,因爲我使用test1.html中的alert call來測試它,以便從php頁面記錄響應。它給了我預期的迴應。

我想我失去了我的變量時代碼執行重新加載test2.php在同一個窗口。我只是不知道如何繞過這個問題。如果可能的話,我想使用POST而不是GET。

任何幫助極大的讚賞。

Little note:Idealy我想使用這個JavaScript和PHP在同一頁面上,但'問題'有PHP,當然首先執行服務器端,然後它運行後js客戶端...

+0

你發送請求,然後你移動到一個新的一頁 - 這樣POST變量不會在那裏。不過,你可以嘗試一個會話。 – Qirel

+0

認爲這可能是問題,事實證明這是問題... –

回答

1

另一種解決方案仍然允許您使用POST,你說你想要的是將信息存儲在會話變量中。會話是一個可用於在請求之間存儲值的對象。見http://php.net/manual/en/book.session.php

Test1.html:

<script type="text/javascript"> 

$(document).ready(function() { 
    var tz = jstz.determine(); // Determines the time zone of the browser client 
    var timezone = tz.name(); //'Asia/Kolhata' for Indian Time. 

$.ajax({ 
     type: 'POST', 
     url: 'test2.php', 
     data: {'timezone': timezone}, 
     cache: false, 
     success: function(){ 
       setTimeout(function() { 
         window.location = 'test3.php'; 
       }, 3000);   } 
    }); 
}); 
</script> 

Test2.php

<?php 
// Start your session (if not already started) 
if (session_status() == PHP_SESSION_NONE) { 
    session_start(); 
} 

// Store posted timezone in the session, which will be available in future calls 
if(isset($_POST['timezone'])) { 
    $_SESSION['timezone'] = $_POST['timezone']; 
} 
else { 
    echo "Fail!"; 
} 

?> 

Test3.php

<?php 
// Start your session (if not already started) 
if (session_status() == PHP_SESSION_NONE) { 
    session_start(); 
} 
if(isset($_SESSION['timezone']) { 
    echo "Your timezone is " . $_SESSION['timezone']; 
} else { 
    echo "Fail!"; 
} 
+0

感謝您的回答。我不熟悉$ _session jet,但是它在我的列表之上,接下來要做,因爲它必須實現以及登錄目的。開始閱讀我猜想的事情的好時機。 –

0

您誤解了您的客戶端和服務器互相交互的流程。您的代碼向test2.php發送POST請求,然後THEN(在您的請求完成時觸發的成功回調)重定向到test2.php。第一次運行test2.php時,它獲取時區POST變量,但是第二次沒有。您可以通過查看瀏覽器開發人員工具中的網絡流量來看到這一點 - 您會看到兩個對test2.php的請求。第一個將返回「時區是...」,第二個(您的瀏覽器顯示)說「失敗!」

有不同的方式得到你想要的東西,但最簡單的將是完全跳過AJAX和剛剛與重定向發送時區沿:

$(document).ready(function() { 
    var tz = jstz.determine(); // Determines the time zone of the browser client 
    var timezone = tz.name(); //'Asia/Kolhata' for Indian Time. 

    // This redirects to test2.php while setting a GET parameter called "timezone" 
    window.location = 'test2.php?timezone='+encodeURIComponent(timezone); 
}); 

<?php 

if(isset($_GET['timezone'])) 
{ 

     $tz = $_GET['timezone']; 
     echo "Timezone is " .$tz; 
} 
else { 
     echo "Fail!"; 
} 

?>