强制浏览器记住选择框中的选项

时间:2013-02-04 23:21:59

标签: javascript jquery html

现在我正在使用

$(document).ready(function(){
        $('#submit-car').click(function(e){
            e.preventDefault();
            var brand = $('#brand option:selected').text();
            var model = $('#model option:selected').text();
            var size = $('#size option:selected').text();
            location.href ='index.php?s='+brand+'+'+model+'+'+size+'';
        });
});

将一些变量发送到网址。我想知道是否有办法强制浏览器记住网站访问者在转到新网址后选择了哪些值。

2 个答案:

答案 0 :(得分:0)

使用会话(保存在服务器上)或cookie(保存在客户端PC上)。

答案 1 :(得分:0)

当用户点击进入下一个网址时,也许你可以使用一些php编码来设置cookies。例如:

<?php
  setcookie("Brand", "brand_value", time()+3600); // Expires in one hour
 ?>

然后在下一页上,您可以检索Brand,如下所示:

<?php echo $_COOKIE["Brand"]; ?> // echoes the value of Brand

更详细你可以试试这个:

<强> Main_File

<script>
$(document).ready(function(){
    $('#submit-car').click(function(e){
        e.preventDefault();
        var brand = $('#brand option:selected').text();
        var model = $('#model option:selected').text();
        var size = $('#size option:selected').text();
        // Ajax call to a cookies.php file, passing the values
        $.get('cookies.php', { thebrand: brand, themodel: model, thesize: size },
        function() {
          // When the call has been completed, open next page
          location.href ='index.php?s='+brand+'+'+model+'+'+size+'';
        });
    });
});
</script>

<强> cookies.php

<?php

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

    // Get all the values
    $theBrand = $_GET["thebrand"];
    $theModel = $_GET["themodel"];
    $theSize = $_GET["thesize"];

    // Call the setTheCookies function, below
    setTheCookies($theBrand, $theModel, $theSize);

    // The setTheCookies function
    function setTheCookies($theBrand, $theModel, $theSize)
    {
        setcookie("Brand", $theBrand, time()+3600);
        setcookie("Model", $theModel, time()+3600);
        setcookie("Size", $theSize, time()+3600);
    }
}

?>

下一页

<?php

  // Get all the values from the next page
  $getBrand = $_COOKIE["Brand"];
  $getModel = $_COOKIE["Model"];
  $getSize = $_COOKIE["Size"];

?>
相关问题