php 如何更改图标颜色添加愿望清单到数据库

pbossiut  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(110)
$('.fa-heart').click(function(){
        if($(this).attr('att') == 0){
            $(this).css('color', 'red');
            $(this).attr('att',1);
        } else {
            $(this).css('color', 'grey');
            $(this).attr('att',0);
        }
    });
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HomeShopping</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" >
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" ></script>
</head>
<body>
        <i class="fas fa fa fa-heart" att="0" style="color: grey"></i>
</body>
</html>

在第一次点击时更改图标并将产品添加到愿望清单数据库
如果我再次点击愿望清单更改图标,产品应该从愿望清单数据库中删除。

fjaof16o

fjaof16o1#

我的评论示例:

Html:

<i class="fas fa fa fa-heart" att="0" style="color: grey"></i>

** AJAX 调用:**

$('.fas').click(function() {
        var val = $(this).val();

        $.ajax({
                    url: "updatefav.php",
                    type: "POST",
                     data: {
                id:val
            },

                    success: function(data){
                if (data == '1') {
                  $('.fas').css('color', 'red');
                }else{
                  $('.fas').css('color', 'grey');
                 }

                    }        
               });

    });

Php:

if(isset($_POST['id'])){

    $id=$_POST['id'];
    $select=$mysqli->prepare("SELECT * FROM YOURTABLE WHERE id=?");
    $select->bind_param('i',$id);
    $select->execute();
    $result=$select->get_result();
    while($res=$result->fetch_assoc()){
    $isfav=$res['wishlist'];
    if($isfav=='1'){  //idk how you improve that so suppose 1=is in wishlist and 0=is not
    $upd=$mysqli->prepare("UPDATE YOURTABLE SET wishlist='0' WHERE id=?");
    $upd->bind_param('i',$id);
    $upd->execute();
    echo '0';
    }else{
    $upd=$mysqli->prepare("UPDATE YOURTABLE SET wishlist='1' WHERE id=?");
    $upd->bind_param('i',$id);
    $upd->execute();
    echo '1';
    }

    }

    }

相关问题