我有兩頁。在第一頁,名爲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客戶端...
你發送請求,然後你移動到一個新的一頁 - 這樣POST變量不會在那裏。不過,你可以嘗試一個會話。 – Qirel
認爲這可能是問題,事實證明這是問題... –