简体   繁体   English

是否有其他选择和重定向来设置会话?

[英]is there an alternative select and redirect to set a session?

This post relates to a previous that i posted but the question is different now: https://stackoverflow.com/questions/5671809/selection-on-change-set-session-not-working/5671816#5671816 这篇文章与我之前发布的文章有关,但现在的问题有所不同: https : //stackoverflow.com/questions/5671809/selection-on-change-set-session-not-working/5671816#5671816

When i select the select field in AJAX the session is set and the page is refreshed to display the value inside the session. 当我在AJAX中选择选择字段时,将设置会话并刷新页面以在会话中显示值。 How can i without a refresh have it where a handle like this 我该如何在不刷新的情况下将其放置在这样的句柄中

$("#output").something

check every second by sending an ajax request to ajax.php?action=checksessionShipping and that ajax file would return a true if the session is set or a false if not set. 通过向ajax.php?action=checksessionShipping发送ajax请求来每秒检查一次,如果设置了会话,则ajax文件将返回true;否则,则返回false。 so once it detects that the session is set (btw, the session would be $_SESSION['shipping'] its on that other stack page.) it returns the value to a div. 因此,一旦它检测到会话已设置(顺便说一句,会话将是其在另一个堆栈页面上的$_SESSION['shipping'] 。),它将返回值到div。

Keep in mind, this is the quick and dirty code. 请记住,这是快速又肮脏的代码。 It should not be used in production as is because it's not very secure or scalable. 由于它不是非常安全或可扩展,因此不应按原样用于生产中。 But, it'll get you started: 但是,它将帮助您开始:

You could do something like this in your HTML: 您可以在HTML中执行以下操作:

<div id="mydiv">

</div><!-- END: #mydiv -->

<script type="text/javascript" charset="utf-8">

    var myInterval = setInterval(function() {

        $.ajax({
          url: 'ajax.php?action=checksessionShipping',
          type: 'POST',
          dataType: 'json',
          data: {},
          complete: function(xhr, textStatus) {
            //called when complete
          },
          success: function(data, textStatus, xhr) {

            if (data.session_set == true)
                $("#mydiv").html(data.shipping);
                clearInterval(myInterval);

          },
          error: function(xhr, textStatus, errorThrown) {
            //called when there is an error
          }
        });

    }, 1000);

</script>

And something like this in your PHP: 在您的PHP中是这样的:

<?php

session_start()
$arrReturn = array('session_set' => FALSE);

if (!empty($_SESSION['shipping'])) {

    $arrReturn['session_set'] = TRUE;
    $arrReturn['shipping'] = $_SESSION['shipping'];

}

echo json_encode($arrReturn);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM