我有两页。在第一页,名为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
认为这可能是问题,事实证明这是问题... –