2017-02-02 52 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!"; 
} 

?>