2016-02-29 66 views
-1

如何在客戶退房後將供應商名稱存儲在cookie中?設置多個Cookie值並獲取SELECT查詢的值

例子:

顧客至上檢查從供應商A的產品和設置cookie的,如:

Cookie: 
+---------+-------+ 
| Name | Value | 
+---------+-------+ 
| vendors | A  | 
+---------+-------+ 

其次從供應商B.檢查出產品

Cookie: 
+---------+-------+ 
| Name | Value | 
+---------+-------+ 
| vendors | A, B | 
+---------+-------+ 

然後我想獲取cookie的所有值爲SELECT name, paymentinfo FROM vendor WHERE name = (All of cookie values),顯示供應商名稱如:

Already Ordered 

Vendor Name : A 
Payment Info : _______________ 

Vendor Name : B 
Payment Info : _______________ 

而且當所有cookie都過期時,它什麼都不會顯示。

回答

0

你應該使用一個循環遍歷你的cookies並獲取值。然後只讀取這些值並將它們傳遞給您的邏輯。我已經添加了幾個JavaScript函數,幾乎只是將它們粘貼到項目中並使用它們。他們應該讓你去,因爲我不清楚你的問題。

function setCookie(cname, cvalue, exdays) { 
    var d = new Date(); 
    d.setTime(d.getTime() + (exdays*24*60*60*1000)); 
    var expires = "expires="+d.toUTCString(); 

    document.cookie = cname + "=" + cvalue + "; " + expires + "; "; 
} 

function getCookie(cname) { 
    var name = cname + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0; i<ca.length; i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1); 
     if (c.indexOf(name) == 0) return c.substring(name.length, c.length); 
    } 
    return ""; 
} 

function editCookie(cname4,cVal){ 
    setCookie(cname4,cVal,365); 

} 


function checkOrSetCookie(cname2, cdata) { 
    var cookie = getCookie(cname2); 
    var rVal; 
    if (cookie != "") { 
     rVal=true; 

    } else { 

     if (cdata != "" && cdata != null) { 
      setCookie(cname2, cdata, 365); 

     }else{ 
      rVal=false; 

     } 
    } 
} 

function deleteCookie(cname3){ 
    document.cookie = cname3+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"; 
} 
function deleteAllCookies() { 

    var cookies = document.cookie.split(";"); 

    for (var i = 0; i < cookies.length; i++) { 
     var cookie = cookies[i]; 
     var eqPos = cookie.indexOf("="); 
     var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; 
     document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; 
    } 
}