开发者

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

开发者 https://www.devze.com 2023-02-25 00:08 出处:网络
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

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

When i select the select field in AJAX the session is set and the page is refreshed to display the value inside the session. 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 s开发者_StackOverflowet. 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.


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:

<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

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

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

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

}

echo json_encode($arrReturn);
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消