向本地存储中的选定ID发出警报(MySQL响应)

Alert selected ID from local storage (response from MySQL)

本文关键字:MySQL 响应 ID 存储      更新时间:2023-11-12

在服务器端-get_notes.php

$rows = array();
while($row = mysqli_fetch_object($result_1)) {
$rows[] = $row;
}
echo json_encode($rows);

输出为-

[{"ID":"1000043","text":"Test"},{"ID:"1000037","text:"Here we go AGAIN!!"}]

JSON由以下脚本加载-

$('#button').click(function(){
var url="get_notes.php";
$.getJSON(url,function(json){
localStorage.setItem('items', JSON.stringify(json));

在客户端,index.html应该从LocalStorage加载JSON数组(之前"我们"在LocalStorage中保存了MySQL响应中的JSON),并向用户显示所有注释。用户可以在index.html上选择笔记后,应用程序应向提醒他所选笔记的ID。

函数,它向我们展示了一个来自LocalStorage的数组:

 var array_list = localStorage.getItem("items");
 alert(array_list);

如何提醒所选笔记上的ID

您是否尝试将值放入console.log()中?JSON字符串中有一个编码的对象数组。你应该先拿到物品。

// Moved from comments below:
var list_json = localStorage.getItem('items');
var list = JSON.parse(list_json) || [];
var index, item;
for (index in list) {
  item = list[index];
  alert(item.ID);
}