How to best interact Javascript with a PHP query

Asked

Viewed 98 times

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 with javascript to call a page from the server, I can only imagine this procedure using ajax, and this would generate multiple calls ajax for each loop item of repetition..... depending on the quantity it can end up being quite time consuming.

  • You cannot load this data at once when the page is executed?

  • 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.

  • Yes, a look at calls using ajax :)

  • Would that be? $. ajax({type:'get'; url:'numero.php', Success: Function(i){$('table tr'). html('<td>'+i+'</td>')}

  • More or less, then when I have more time I answer, if someone doesn’t answer first

  • 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.

  • 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.

  • Sorry, I read running your reply, I will give the vote of aggression. Thank you

  • You know how to echo in PHP if called the PHP file with ficheiro?id=21 for example?

  • 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.

  • That mistake of mysqli_fetch_array is not about what is in the ajax Success code. You can put the PHP code you have?

  • 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.

  • It is worth noting that there is an error here: localStorage.getItem(i); + '</p That one ; There’s no sense in the middle.

  • 1

    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.

  • Cool, missed the - or die(mysql_error()) - Thank you to all who helped me, you are grade 10.

Show 11 more comments
No answers

Browser other questions tagged

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