Hide or show buttons based on database entry

Hello everyone,
I am trying to enter a simple code to hide a button on a dynamic page, but it doesn’t work for me. I’m already desperate. I also looked for similar topics, for example this, but it still doesn’t work for me. (https://forum.wixstudio.com/t/hide-or-show-buttons-based-on-database-entry/32920)

I have a database of items that visitors can reserve and I need the item to be marked as already reserved after filling out a Wix Form. At the same time, I would like the button for Reserving an item to be hidden, for those items that are no longer available. Everything is connected to the database and displayed using a dynamic page.

I tried these two procedures:
1)
Add a checkbox to the page that is connected to the database and shows me which item is available and which is no longer available. I then added this code to $w.onReady(function () …

if ($w(“#checkbox2”).checked === true) {
$w(“#button15”).hide();
}
It doesn’t work, button15 is always hidden

I also tried this code, checking the availability of the item directly through the database:

$w(“#ydynamicDataset”).onReady(() => {
var item = $w(“#dynamicDataset”).getCurrentItem();
if (item[“spare”] === false) {
$w(“#button15”).show();
} else {
$w(“#button15”).hide();
}
});

It’s not functional either.

At the same time, I have a Form on the WIX page and an added checkbox field connected to the CMS. When I check the checkbox and submit the form, I would like the item to be marked in the database as unavailable = reserved. I have a boolean column there. I thought it would go automatically, but it doesn’t do anything. I guess some code has to be added.

Thanks in advance for any help.

Just in case anyone else reading this post wants to check for an empty field in their database……

let item = $w(“#dynamicDataset”).getCurrentItem().yourFieldKey;

I am going to assume all of your element IDs and dataset name are correct. In your code, check for a positive value.

if (item){

So if there is something there, hide or else show. If you search for empty it could return too many possibilities. Such as null, undefined, false.

For the logic to work, your button needs to be set to hidden on load. That way it only shows up if there is something in that field.

Thank you for the explanation. I reversed the condition and now everything works. I have the button set as hidden on load. Only displayed when the condition is true.

Now I only need to solve the second part of the problem, how to write in the database, after filling out the form, that the item is reserved. This code doesn’t work for me:

export function button16_click(event) {
$w(“#dynamicDataset”).setFieldValue(“spare”, true);
$w(“#dynamicDataset”).save();
}

You should probably create a new post with a question for your second issue. Include screenshots, the way your form is set up, full code for your form, dataset permissions, database permissions, etc.

It can literally be anything that is causing your problem. The more details the better. Them the community can try and help point you in the right direction.