0
Hello
I have a javascript function that reads the browser localStorage that represents the client ID and displays the result of the localStorage read in a DIV. I would like javascript to bring the ID’s of the clients allocated nolocalStorage and collect from the database using a PHP query the client’s name and general data, as the FOR loop runs.
But I’ve never done it before, what better way to be doing it, the right way. If possible, give me an example.
I’ve come this far:
function listarClientes()
{
for(i = 0; i <= 10; i++)
{
// Retrieve
var divInformacoesClientes = document.getElementById("divCliente");
divInformacoesClient.innerHTML += '<p>' + localStorage.getItem(i); + '</p>';
}
}
Current code with AJAX request.
$.get("carrinho_compras.php",
{id:compras_no_carrinho},
function(data)
{
//alert(data);
console.log(data);
document.getElementById("total").innerHTML += data ;
}, "html");
PHP code
<?php
include 'include/connection.php';
$id = $_GET['id'];
$query = "SELECT id, imagem, produto FROM produtos WHERE id = $id ORDER BY id ASC";
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result))
{
echo $row[2];
}
?>
Error that is displayed when inserting ( += ) to concatenate all values received by AJAX request.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\projetos\PaperList\carrinho_compras.php on line 11
Thank you.
I recommend thinking of another way, pulling the data from
localStorage
withjavascript
to call a page from the server, I can only imagine this procedure usingajax
, and this would generate multiple callsajax
for each loop item of repetition..... depending on the quantity it can end up being quite time consuming.– MarceloBoni
You cannot load this data at once when the page is executed?
– MarceloBoni
So, I’m making a shopping cart and as the person is buying it will postpone in the cart, in the case in the localStorage, this worked very well, I just need to get the product and customer data with a query in php The system is ready, I don’t want to have to change everything. Maybe I have to use prototype.
– abduzeedo
Yes, a look at calls using ajax :)
– MarceloBoni
Would that be? $. ajax({type:'get'; url:'numero.php', Success: Function(i){$('table tr'). html('<td>'+i+'</td>')}
– abduzeedo
More or less, then when I have more time I answer, if someone doesn’t answer first
– MarceloBoni
In cso I will have a PHP page with the query and ajax goes in this page and shows the return of the page. How to get the prototype.
– abduzeedo
I see you ended up wearing
<p></p>
as I suggested in another question, You can always give a vote of thanks since you didn’t mark it as the right answer. I say not just for myself, but as a practice with other members of the community.– Sergio
Sorry, I read running your reply, I will give the vote of aggression. Thank you
– abduzeedo
You know how to
echo
in PHP if called the PHP file withficheiro?id=21
for example?– Sergio
So, I already managed to make the Ajax request, but when I enter += to concatenate all the values received by the AJAX request sent by the PHP page, it gives a strange mysqli error. I updated my post above.
– abduzeedo
That mistake of
mysqli_fetch_array
is not about what is in the ajax Success code. You can put the PHP code you have?– Sergio
So, the strange thing is that only from the error, when I enter the ( += ) when I enter just the ( = ), it works normally. I changed my post up there.
– abduzeedo
It is worth noting that there is an error here:
localStorage.getItem(i); + '</p
That one;
There’s no sense in the middle.– Bacco
On the error of the question, it has already been answered: http://answall.com/questions/28184/70 - following the steps of the answer, you will find the reason for the error.
– Bacco
Cool, missed the - or die(mysql_error()) - Thank you to all who helped me, you are grade 10.
– abduzeedo