2013-02-04 20 views
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。我想知道是否有辦法強制瀏覽器記住網站訪問者在轉到新網址後選擇的值。

+0

這就是爲什麼餅乾被髮明.. –

+0

您可以使用Cookie或本地存儲。 –

回答

0

使用會話(保存在服務器上)或cookie(保存在客戶端PC上)。

0

也許你可以使用一些PHP編碼來設置cookies當用戶點擊去下一個網址。例如:

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

那麼接下來的頁面上,你可以檢索品牌,如:

<?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"]; 

?>