Page according to the clicked product ID (JAVASCRIPT)

Asked

Viewed 249 times

0

I’m trying to get the ID of the boot which is a various rival and play on another page to perform a fetch API. Only this button is done automatically, it depends on how many purchases have in the customer’s Cpf, if there are 2 purchases, will be two buttons.

There for each button will be generated an id of the button automatically, which will be the id of the product that is in the database.

Then when I click on the button, it should direct to another page, and should get the id of the button clicked, but when I click on the button, it’s taking the last ID of the product purchased from the customer, and not that id I clicked on.

First js of page 1:

                var names = [];
                names[0] = element.ID;
                localStorage.setItem("names", JSON.stringify(names));

             $('.produto-1').append(`<div class="col-xs-6 col-sm-4 col-lg-12">
             <div class="thumbnail">
                 <div class="caption">
                     <h3>${element.PRODUTO}</h3> <img class="fon" src="img/icon/guido.ico"/>
                     <p class="flex-text text-muted">${element.STATUS}</p>
                     <br>
                        <p><a class="btn btn-primary m" type="button" id="${element.ID}" value="${element.ID}" onClick="${element.ID}" href="progress.php">Acompanhar Pedido</a></p>                           
                        </div>
             </div>
         </div>`);

I created this class produto-1 to create the message showing the product name, its status and mount the button with the product id.

So if you happen to have 2 products, this is automatically creating there in this code.

In the code:

var names = [];
names[0] = element.ID;
localStorage.setItem("names", JSON.stringify(names));

I get the product ID.

When you click the button you will direct to the page2 and in it I have the following js:

    function Progress() {

    var storedNames = JSON.parse(localStorage.getItem("names"));
    console.log(storedNames);

      jQuery.support.cors = true;

            //  Default options are marked with *
             fetch('http://API_AQUI', {
                 method: 'POST',
                 headers: {'Content-Type':'application/x-www-form-urlencoded'},
                 body: `ID=${storedNames}`

             }).then(response => response.json().then(data => ({
                 data: data,
                 status: response.status
             })
             ).then(res => {
                 res.data.map(element => {
                     var text = element;
                     console.log(text);

                     $('.progresso-1').append(`${element.NOME}`);
                     $('.progresso-2').append(`${element.PRODUTO}`);
                     $('.progresso-3').append(`${element.CPF}`);
                     $('.progresso-4').append(`${element.CIDADE}`);
                     $('.progresso-5').append(`${element.ENTREGAPREVISTA}`);
                     $('.progresso-6').append(`${element.CODPRODUTO}`);
                     $('.progresso-7').append(`${element.CODCLIENTE}`);
                     $('.progresso-8').append(`${element.ENTREGAPROGRAMADA}`);
                     $('.progresso-9').append(`${element.NOTAFICAL}`);
                     $('.progresso-10').append(`${element.QTDPEDIDO}`);
                     $('.progresso-11').append(`${element.ENTREGACONFIRMADA}`);
                     $('.progresso-12').append(`${element.MONTAGEMPREVISTA}`);
                     $('.progresso-13').append(`${element.MONTCONFIRMADA}`);

            })
        })
    )

}

The var storedNames = JSON.parse(localStorage.getItem("names")); picks up the product ID, but is not picking up the clicked product id, but always the last ID of all products the customer has.

Is there any way I can solve this problem?

  • the button is directed to a page, as has been said by href, and I put the variable id in the id id of the button. I did not event that picks the click

  • By localStorage, but it does not send in what was clicked, but in the last id that is in db

  • No, the query does it correctly, until pq I did tests on the bd itself and on Postman to test the api. The button is only sending the id 8, in case the number 8. The page that shows the data is a dynamic page, it is only 1 page made, there depending on the button clicked, it shows certain data

  • The map will go through the array, and the localStorage.setItem('pegaId', pegaId); it will always be the last value of the array, because at each iteration in the array, the value of the localStorage is changed, getting the value of the last item

  • 1

    As the dvd said, your localStorage.setItem is only overriding values. Take a look at this example. https://stackoverflow.com/questions/3357553/how-do-i-store-an-array-in-localstorage

  • @Joséfrancisco, I tried to do it that way, but when he goes he still keeps getting the value 8, I must not know how to pick with the boot

  • @dvd then what would it be like to get the value clicked? I tried to do several times, and always only shows the last value of the array

  • @Maria could complement the code of her first JS, in which you are populating the localstorage?

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.