Check products with ajax and php for sales page

Asked

Viewed 50 times

0

I’m trying to create a small sales system for a local store and I’m having trouble on the sales page, I was trying to learn how to use ajax to query product data in the mysql database of xampp and then return the value and name of the product, to be able to put the amount that is being sold and add with the other products that the customer is taking and in the end update the quantity in the database, however I am not able to pull the data from the database. I’d like a little help so I can see what’s wrong with the code. [! [The code displays a Warning saying that Undefined array key "codigoBarra"][1]][1]

Sales

Sale

Barcode:

recoverProduct = () =>{ var product = new Xmlhttprequest(); var codigoBarra = Document.getElementById("codigoBarra"). value; var outcome = Document.getElementById("result"); open product("POST"+"sale.php? codeBarra="+codeBarra, true);
    if(produto.readyState == 4){

        if(produto.status == 200){
            result.innerHTML = xmlreq.responseText;
         }
         else{
             result.innerHTML = "Erro: " + xmlreq.statusText;
         }
        }
        produto.send("venda.php");
    };
include('conecta.php'); $codigoBarra = $_POST['codigoBarra']; $sql = "select nome, valor from produto where codigoDeBarra = ?"; $resultado = $pdo->prepare($sql); $resultado->execute(); $vetor=$resultado->fetchall(PDO::FETCH_OBJ); foreach($vetor as $linha){ echo $linha->nome."--".$linha->valor; }

?>

Here’s the html, javascript and php code I’m using. the database used is phpmyadmin’s mysql. table structure: id_product int not null auto_increment, Code to sweep up, name varchar, int amount, double value.

If anyone can help I would be very grateful ! [1]: https://i.stack.Imgur.com/auF45.png

  • 1

    Is the code complete? By q you don’t seem to be passing the variable $codigoBarra for your query. Is missing $resultado->bindValue() probably. Take a look here

1 answer

0


In your own example the error is implicit in your problem that it is not finding the index 'code'.

Accessors should never be used directly if your array / object is fully changeable and dynamic.

So I thought I’d better do it

   if (isset($array['codigoDeBarras']) {
     /// faz algo
    } else {
 // faz algo se o indice não existe
}

In PHP 8 it has excellent functions like match that can simplify your code as well.

Browser other questions tagged

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