用jquery传递变量,打开php页面并选择它

用jquery传递变量,打开php页面并选择它,php,jquery,Php,Jquery,我有两页。在名为test1.html的第一页上,我尝试检索用户时区。我想将它发送到一个名为test2.php的php页面,并加载该页面,而不是带有可变时区的test1。这是代码。 Test1.html: <script type="text/javascript"> $(document).ready(function() { var tz = jstz.determine(); // Determines the time zone of the browser clie

我有两页。在名为test1.html的第一页上,我尝试检索用户时区。我想将它发送到一个名为test2.php的php页面,并加载该页面,而不是带有可变时区的test1。这是代码。 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 = 'test2.php';
                }, 3000);//this will redirct to somefile.php after 3 seconds
        }
    });
});
</script>
<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

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

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

?>
<?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!";
}

?>
在test2.php的pageload上,我只得到“Fail!”消息jquery和php部分工作正常,因为我在test1.html中使用了一个警报调用对其进行了测试,以记录php页面中的响应。它给了我预期的回应

在同一窗口中执行代码重新加载test2.php时,我想我丢失了变量。我只是不知道如何绕过这个问题。如果可能的话,我想使用POST而不是GET

非常感谢您的帮助


小提示:理想情况下,我想使用这个javascript和php在同一个页面上,但问题是php当然是先在服务器端执行,然后在客户端运行…

您误解了客户端和服务器之间交互的流程。您的代码向test2.php发送POST请求,然后在请求完成时触发的成功回调中重定向到test2.php。第一次运行test2.php时,它会获取timezone POST变量,但第二次则不会。您可以通过查看浏览器开发工具中的网络流量看到这一点——您将看到对test2.php的两个请求。第一个返回的时区是…,第二个显示的时区是Fail

有不同的方法可以获得您想要的,但最简单的方法是完全跳过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!";
}

?>

另一个仍然允许您使用POST的解决方案是将信息存储在会话变量中,这是您说过希望使用的。会话是一个对象,可用于在请求之间存储值。看

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 = 'test2.php';
                }, 3000);//this will redirct to somefile.php after 3 seconds
        }
    });
});
</script>
<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

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

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

?>
<?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!";
}

您发送请求,然后移动到一个新页面,这样POST变量就不在那里了。不过,你可以试着开个会。虽然你认为这可能是问题所在,但事实证明这就是问题所在……谢谢你的回答。我不熟悉$_会话jet,但它是我下一步要做的最重要的事情,因为为了登录,它也必须实现。我想是时候开始读一些东西了。