retrieve array from localstorage and turn it into a sql query in php


I'm developing a feature to add and remove favorite properties with localstorage.

//Com esse código eu recupero os itens que foram armazenados em um array no localstorage
               var favoritos = JSON.parse(localStorage.getItem("favoritos"));
               $(favoritos).each(function(i,v) {
                   $('#favoritos').append("<li>" + v + "</li>");

The return I get from the favorites variable when I run a document.write , are the property references separated by (,). Ex: 3174,3304.3205. That's exactly what I need to build a SQL query(SELECT * FROM properties WHERE id_imovel IN ($favorites)).

How to transform the retrieved items from localstorage into something recognized by php to store these items in a variable($favorites)?


You must loop a result requested by AJAX to a PHP controller and in this request you have sent the content of localStorage.getItem("favoritos") which will be processed by PHP.

Summing up:

  1. Interface requests controller informing list of favorite IDs;
  2. Controller delivers list of items in a JSON from a STRING of comma-separated IDs
  3. Interface Loops the List

There will be two requests in total: Main document and JSON

Scroll to Top