retrieve array from localstorage and transform into a sql query in php

3

I'm developing a feature for adding and removing favorite properties with localstorage.

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

The return I get from the favorite variable when I run a document.write , are the property references separated by (,). Ex: 3174,3304,3205. This is exactly what I need to mount a SQL query (SELECT * FROM imVEL WHERE id_imovel IN ($ favorites)).

How do I turn items retrieved from localstorage into something recognized by php to store these items in a variable (favorite $)?

    
asked by anonymous 26.08.2015 / 15:30

1 answer

1

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

In short:

  • Interface requests controller reporting list of favorite IDs;
  • Controller delivers list of items in a JSON from a STRING of comma-separated IDs
  • Interface Looping in List
  • There will be two requisitions to the total: Main Document and JSON

        
    26.08.2015 / 16:11