i started making a wishlist but i dont know how to do the following:
- a button which add/remove items from database when clicked
- display the favourites
@app.route("/add_favourite", methods=["POST"]) # / contact page
def add_favourite():
if request.method == "POST":
TablePk = request.form.get("TablePk")
Id = request.form.get("Id")
CustomerId = request.form.get("CustomerId")
dbObject = Database(DB_FILE)
# Check if that is already added
# May be remove favourite
dbObject.addFavourite(TablePk, Id, CustomerId)
return "<span class='badge text-bg-success'>Added!</span>"
def addFavourite(self, TablePk, Id, CustomerId):
cursor = self.connection.cursor()
query = f"INSERT INTO Favourite ({TablePk}, 'CustomerId') VALUES ({Id}, {CustomerId});"
#print(query)
try:
cursor.execute(query)
self.connection.commit()
return True
except:
print("Something went wrong")
$(".favourite-btn").on("click", function (e) {
e.preventDefault();
$(".badge").hide("slow");
var button = "#" + $(this).attr('id');
console.log(button);
var data = { "TablePk":$(button).attr("data-table"), "Id":$(button).attr("data-id"), "CustomerId":$(button).attr("data-user") }
console.log(data);
$.ajax({
url: "/add_favourite",
data: data,
type: "POST",
success: function (response) {
$(button).after(response);
setTimeout(function () {
$(".badge").hide("slow");
}, 3000);
}
});
});
i attempted to create a function to remove the code but it didnt work