gpt4 book ai didi

javascript - 如何使用 cookie 将变量传递到下一页

转载 作者:IT王子 更新时间:2023-10-29 00:05:53 25 4
gpt4 key购买 nike

我正在设计一个房地产网站。我的网站上有很多广告,感谢我的 friend Arsh Singh,我在每个帖子上创建了一个“收藏夹”或“保存”按钮,这些按钮将根据 cookie 将所选页面标题保存在特定页面中,供用户查看帖子当他或她想要的时候。
现在我想在用户点击“添加到收藏夹”时将广告的 ID 发送到收藏夹页面,因此我可以根据 ID 从数据库中获取特定的广告数据。
我可以这样做吗?如何?这是我当前的代码,它只能将页面标题发送到收藏夹页面。有什么想法吗?

<!DOCTYPE html>
<html>
<head>
<title>New page name</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src=favoritecookie.js></script>
</head>
<body>
<a href="javascript:void(0);" id="addTofav">Add me to fav</a>
<ul id="appendfavs">

</ul>

<?php
error_reporting(0);
include("config.php");
(is_numeric($_GET['ID'])) ? $ID = $_GET['ID'] : $ID = 1;
$result = mysqli_query($connect,"SELECT*FROM ".$db_table." WHERE idhome = $ID");
?>
<?php while($row = mysqli_fetch_array($result)):
$price=$row['price'];
$rent=$row['rent'];
$room=$row['room'];
$date=$row['date'];
?>
<?php
echo"price";
echo"room";
echo"date";
?>
<?php endwhile;?>

</body>
</html>

//favoritecookie.js
/*
* Create cookie with name and value.
* In your case the value will be a json array.
*/
function createCookie(name, value, days) {
var expires = '',
date = new Date();
if (days) {
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = '; expires=' + date.toGMTString();
}
document.cookie = name + '=' + value + expires + '; path=/';
}
/*
* Read cookie by name.
* In your case the return value will be a json array with list of pages saved.
*/
function readCookie(name) {
var nameEQ = name + '=',
allCookies = document.cookie.split(';'),
i,
cookie;
for (i = 0; i < allCookies.length; i += 1) {
cookie = allCookies[i];
while (cookie.charAt(0) === ' ') {
cookie = cookie.substring(1, cookie.length);
}
if (cookie.indexOf(nameEQ) === 0) {
return cookie.substring(nameEQ.length, cookie.length);
}
}
return null;
}
/*
* Erase cookie with name.
* You can also erase/delete the cookie with name.
*/
function eraseCookie(name) {
createCookie(name, '', -1);
}

var faves = new Array();

function isAlready(){
var is = false;
$.each(faves,function(index,value){
if(this.url == window.location.href){
console.log(index);
faves.splice(index,1);
is = true;
}
});
return is;
}

$(function(){
var url = window.location.href; // current page url
$(document.body).on('click','#addTofav',function(e){
e.preventDefault();
var pageTitle = $(document).find("title").text();
if(isAlready()){
} else {
var fav = {'title':pageTitle,'url':url};
faves.push(fav);
}
var stringified = JSON.stringify(faves);
createCookie('favespages', stringified);
location.reload();
});
$(document.body).on('click','.remove',function(){
var id = $(this).data('id');
faves.splice(id,1);
var stringified = JSON.stringify(faves);
createCookie('favespages', stringified);
location.reload();
});

var myfaves = JSON.parse(readCookie('favespages'));
if(myfaves){
faves = myfaves;
} else {
faves = new Array();
}
$.each(myfaves,function(index,value){
var element = '<li class="'+index+'"><h4>'+value.title+'</h4> <a href="'+value.url+'">Open page</a> '+
'<a href="javascript:void(0);" class="remove" data-id="'+index+'">Remove me</a>';
$('#appendfavs').append(element);
});
});

最佳答案

Cookie 中的 JSON您可以使用 JSON 通过序列化 JSON 将详细信息(id、帖子名称等)存储到 cookie 中: jquery save json data object in cookie

但是,为了安全起见,您不应将数据库表名存储在 cookie 中。

PHP cookie 访问 https://davidwalsh.name/php-cookies

关于javascript - 如何使用 cookie 将变量传递到下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com