1

现在我正在使用

$(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+'';
        });
});

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

4

2 回答 2

0

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

于 2013-02-04T23:26:07.827 回答
0

当用户点击转到下一个 url 时,也许您可​​以使用一些 php 编码来设置cookie 。例如:

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

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

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

更详细地说,你可以试试这个:

主文件

<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>

饼干.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"];

?>
于 2013-02-04T23:27:07.130 回答