【问题标题】:save page variables in local storage and show them in another page将页面变量保存在本地存储中并在另一个页面中显示它们
【发布时间】:2016-11-08 05:03:25
【问题描述】:

我有一个广告网站,我在图片库中展示我的所有广告,如果您点击每个广告,它会转到查看更多页面,您可以获得有关该广告的更多数据。

viewmore page
<?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'];
$room=$row['room'];
$parking=$row['parking'];
$floor=$row['floor'];
?>

我有这样的 php 代码。现在我想在我的查看更多页面中添加一个选项,如果用户单击一个按钮,例如将此页面添加到收藏夹,页面变量"price", "room", "parking", "floor"保存在本地存储中并显示在我命名为收藏页面的某些页面上,并且每次用户来到网站,他或她可以轻松地访问页面并查看所选页面,知道吗?

【问题讨论】:

  • 你可以在这里查看我的答案:Save favorite page in cookies,基于同样的想法,它包括,add page,remove page,list pages
  • 嗨,谢谢,我将您的答案添加到问题中。你确定它是正确的吗,因为我将它添加到我的 websie 并且它根本不起作用。我点击添加我到收藏,它没有显示任何东西。
  • 你确定 ajax.googleapis.com/ajax/libs/jquery/2.2.2 是正确的
  • 对我来说它一直有效,直到现在,向我展示您的控制台错误,并记住,您必须根据需要对代码进行少量编辑。否则它不会像你期望的那样工作。
  • 我没有在你的代码中添加任何东西,我只是想先检查一下,然后按照我想要的方式编辑它。它也没有给我任何错误,我点击它,它没有显示任何内容。我在 google chrome 和 firefox 中检查它

标签: javascript php


【解决方案1】:

你也可以使用LocalStorage:

localStorage 属性允许您访问本地存储对象。存储在 localStorage 中的数据没有过期时间。在 HTML5 中,localStorage 对象是按域隔离的。

一个例子:

// Save data to the current local store
localStorage.setItem("username", "John");

// Access some stored data
alert( "username = " + localStorage.getItem("username"));

// Remove some data from Local Storage
localStorage.removeItem('username');

//json example
var car = {};
car.wheels = 4;
car.doors = 2;
car.sound = 'vroom';
car.name = 'Lightning McQueen';
console.log( car );
localStorage.setItem( 'car', JSON.stringify(car) );    //converts the 'car' object into a string and attaches it to the property 'car' of localStorage
console.log( JSON.parse( localStorage.getItem( 'car' ) ) );  //logs an object

您可以点击以下链接了解更多关于本地存储使用的信息:

https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage

https://www.smashingmagazine.com/2010/10/local-storage-and-how-to-use-it/

一个例子:

http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_webstorage_local_clickcount

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多