How to put a variable alert in the onclick parameter of a link? using jquery and php

1

I need to put a product message unavailable on my system, which takes the available status of the db (YES or NO) I did an IF that changes the image of the product when the status is NAO but I want a message if the user clicks on it saying that the same is unavailable ..

The products are pulled as a list of the bank and everyone already has a confirmation alert when a user clicks, but I need them to change the message if the item is unavailable

Here's what I have: Display the products like this:

<a href="cadastra.php?cod=<?php echo $id_pro ?>&nome=<?php echo $nomepro ?>&preco=<?php echo $precopro ?> class="btn twitter" onclick="return confirm('<?php echo $nomepro ?> - CONFIRMA O PEDIDO?')">

<h3 class="heading-title"><?php echo $nomepro ?>&nbsp R$<?php echo $precopro ?></h3> </a>

The IF that changes the image when unavailable is like this:

 $sqlp = mysql_query("SELECT * FROM produtos WHERE id_categoria='11'");
    while($verp = mysql_fetch_array($sqlp)){
        $disponivel = $verp['disponivel'];
        $id_pro = $verp['cod'];
        $nomepro = $verp['nome'];
        $descpro = $verp['descricao'];
        $precopro = $verp['preco'];





        if($disponivel == SIM){
            $img = "../imagens/disponivel.png"; 


        }else{
        $img = "../imagens/indisponivel.png";

        }

I'm using jquery (without knowing) if anyone has any ideas, Thanks!

    
asked by anonymous 30.03.2017 / 02:57

1 answer

0

You can simply do this:

...
while($verp = mysql_fetch_array($sqlp)){
    if($rs['disponivel']){
        $estado = "inactivo";
    } else {
        $estado = "activo";
    }
print "<a href=\"#\" name=\"{$rs['cod']}\" class=\"item {$estado}\">{$rs['nome']}</a><br>";
}
?>

<script>
(function(){
    var el = document.querySelectorAll('a.item');

    el.forEach(function(a){
        a.addEventListener('click', function(m){
            if(a.classList.contains('inativo')){
                m.preventDefault();
                alert('inativo');
            }

        }, false);
    });

})();
</script>

The disponível field could be a 1 or 0 .

    
30.03.2017 / 07:33