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

Asked

Viewed 190 times

1

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

Products are pulled as bank list and everyone already has a confirmation Alert when a user clicks, but need it to change the message if the item is unavailable

Here’s what I got: Show 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>

IF changing the image when unavailable is so:

 $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 am using jquery (without knowing) if anyone has any ideas, Thank you!

1 answer

0

You can just do it like 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 fielddisponível could be a 1 or 0.

Browser other questions tagged

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