# How can I check a box and update the database with the new value?

**URL:** <https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248>\
**Category:** Ask the community\
**Tags:** wix-members-area, code\
**Created:** [November 3, 2024, 5:55pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248 "2024-11-03T17:55:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 3, 2024, 5:55pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/1 "2024-11-03T17:55:50Z")

</div>

I have 6 checkbox-elements, each for a specific language and have tried with different codes to update the database when one of the boxes are checked but with no result. This is my latest code-version so please help me guessing what is wrong?

wixData.get(“Members”, userId)  
$w(‘#checkbox1’).onClick((event) =\> {  
let isChecked = $w(“#checkbox1”).checked;  
if(isChecked === undefined) {  
console.log(“is not checked”);  
} else {  
let toUpdate = {  
english: true,  
spanish: false,  
french: false,  
german: false,  
swedish: false,  
arabic: false,  
};  
wixData  
.update(“Members”, toUpdate)  
.then((results) =\> {  
$w(‘#dataset5’).save()  
console.log(results);  
setTimeout(() =\> {  
wixLocation.to(`/members/account/${userId}`);  
}, 500);  
})

The reason to why I don´t use a checkboxgroup is that I wanna use boolean-values for a function that also has with the languages to do. Unfortunately its not possible to connect such a box with a boolean-field and therefore I use single boxes.

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [November 4, 2024, 1:32am UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/2 "2024-11-04T01:32:38Z")

</div>

Hi, Kristofer\_Bath !!

Hello! There are a few things to point out, but the main reason the data isn’t saving is that the `toUpdate` object is missing the item ID (`_id`). The standard approach is to retrieve the member’s data first (including the `_id`) with `wixData.get("Members", userId)`. Then, modify the parts of the data you want to change, and pass this updated data as `toUpdate` into `wixData.update("Members", toUpdate)`. 🙌 😉

```javascript
import wixData from "wix-data";

/* existing item:
 *
 * {
 * "_id": "00001",
 * "title": "Mr.",
 * "first_name": "John",
 * "last_name": "Doe"
 * }
 *
 */

wixData
  .get("myCollection", "00001")
  .then((item) => {
    item.last_name = "Smith"; // updated last name
    wixData.update("myCollection", item);
    console.log(item); //see item below
  })
  .catch((err) => {
    console.log(err);
  });

/* updated item:
 *
 * {
 * "_id": "00001",
 * "title": "Mr.",
 * "first_name": "John",
 * "last_name": "Smith"
 * }
 *
 */

```

from: [https://dev.wix.com/docs/velo/api-reference/wix-data/update](https://dev.wix.com/docs/velo/api-reference/wix-data/update)

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 4, 2024, 7:53am UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/3 "2024-11-04T07:53:37Z")

</div>

> [@onemoretime](#):
>
> ```javascript
> .get("myCollection", "00001")
> .then((item) => {
> item.last_name = "Smith"; // updated last name
> 
> ```

Thanks for the tip but do I really have to add the "item.last\_name = “Smith”; part? (but then ofcourse with the languages instead). Maybe I should write like this there?

wixData.get(“Members”, userId)  
.then((item) =\> {  
item.english = $w(“#checkbox1”)  
item.spanish = $w(“#checkbox2”)  
item.german = $w(“#checkbox3”)  
item.french = $w(“#checkbox4”)  
item.swedish = $w(“#checkbox5”)  
item.arabic = $w(“#checkbox6”)

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [November 4, 2024, 8:11am UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/4 "2024-11-04T08:11:16Z")

</div>

It would be better to use `$w("#checkBox1").value` to get a boolean value. 😉

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 4, 2024, 9:54am UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/5 "2024-11-04T09:54:09Z")

</div>

Do you mean that I should write like this?

wixData.get(“Members”, userId)  
.then((item) =\> {  
item.english = $w(“#checkbox1”).value  
item.spanish = $w(“#checkbox2”).value  
item.german = $w(“#checkbox3”).value  
item.french = $w(“#checkbox4”).value  
item.swedish = $w(“#checkbox5”).value  
item.arabic = $w(“#checkbox6”).value

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 4, 2024, 1:39pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/6 "2024-11-04T13:39:47Z")

</div>

Another question is if it´s wrong to write like this, with several setFieldValues in a row?

wixData  
.update(“Members”, toUpdate)  
.then((results) =\> {  
$w(“#dataset5”).setFieldValue(“english”, true), $w(“#dataset5”).setFieldValue(“spanish”, false), $w(“#dataset5”).setFieldValue(“german”, false),  
$w(“#dataset5”).setFieldValue(“french”, false), $w(“#dataset5”).setFieldValue(“swedish”, false), $w(“#dataset5”).setFieldValue(“arabic”, false),  
$w(‘#dataset5’).save()

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [November 4, 2024, 2:44pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/7 "2024-11-04T14:44:42Z")

</div>

I think it’s better to choose either `wix-data` or Dataset to work with. Everything that can be done with Dataset can also be achieved with `wix-data`. However, there are certain scenarios where using Dataset can simplify the implementation. If you’re unsure how to use both, there’s no need to force yourself to use both. The important thing is to find a pattern that you can understand thoroughly and consistently get right.

I mostly use `wix-data`, but I use Dataset when reading data from a collection to display in a repeater, as it’s quite convenient in these cases. While `wix-data` and Dataset can achieve the same outcomes, they operate differently and independently, and this difference can sometimes make it challenging to synchronize their behavior. So, combining them can lead to errors, and it’s usually best to stick with one approach.

Back to the main question—there’s a method called `setFieldValues()` that might be helpful. I’ll include a link for reference. 🙌

[https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-values](https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-values)

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 4, 2024, 3:48pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/8 "2024-11-04T15:48:34Z")

</div>

Thanks for the advice but you missed that I had already asked wheter if its okay to write like this when it comes to setFieldValue? (with several in a row) 🙂

wixData  
.update(“Members”, toUpdate)  
.then((results) =\> {  
$w(“#dataset5”).setFieldValue(“english”, true), $w(“#dataset5”).setFieldValue(“spanish”, false), $w(“#dataset5”).setFieldValue(“german”, false),  
$w(“#dataset5”).setFieldValue(“french”, false), $w(“#dataset5”).setFieldValue(“swedish”, false), $w(“#dataset5”).setFieldValue(“arabic”, false),  
$w(‘#dataset5’).save()

---

<div class="post-metadata">

**Author:** ![onemoretime](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/onemoretime/32/104023_2.png) [@onemoretime](https://forum.wixstudio.com/u/onemoretime)\
**Post date:** [November 4, 2024, 3:55pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/9 "2024-11-04T15:55:22Z")

</div>

`setFieldValue()` and `setFieldValues()` are different. 🤔

```javascript
wixData.update("Members", toUpdate)
.then((results) => {
    $w("#dataset5").setFieldValues({
        "english": true,
        "spanish": false,
        "german": false,
        "french": false,
        "swedish": false,
        "arabic": false
    });
    $w("#dataset5").save();
});

```

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 6, 2024, 10:00am UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/10 "2024-11-06T10:00:08Z")

</div>

Okay, how strange because here they have written it as setFieldValue:

[https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-value](https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-value)

Is that only ment for a single element and one database field?

Anyhow, now I have written the full code like this but the system still can´t find out who is the current logged in user:

wixData.get(“Members”, userId)  
$w(‘#checkbox1’).onChange((event) =\> {  
$w(‘#dataset5’).getCurrentItem()  
let isChecked = $w(“#checkbox1”).checked;  
if(isChecked === undefined) {  
console.log(“is not checked”);  
} else {  
let toUpdate = {  
english: true,  
spanish: false,  
french: false,  
german: false,  
swedish: false,  
arabic: false,  
};  
wixData.get(“Members”, userId)  
.then((item) =\> {  
item.english = $w(“#checkbox1”).value

```
wixData.update("Members", toUpdate)
    .then((results) => {
     $w("#dataset5").setFieldValues({
        "english": true,
        "spanish": false,
        "german": false,
        "french": false,
        "swedish": false,
        "arabic": false
});
$w("#dataset5").save();
     console.log(results);
   setTimeout(() => {
     wixLocation.to(`https://daddymate.com/members/account/${userId}`);
}, 500);

```

})

---

<div class="post-metadata">

**Author:** ![Mark\_Hunte](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mark_hunte/32/100172_2.png) [@Mark\_Hunte](https://forum.wixstudio.com/u/Mark_Hunte)\
**Post date:** [November 6, 2024, 2:59pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/11 "2024-11-06T14:59:59Z")

</div>

> [@Kristofer\_Bath](#):
>
> Okay, how strange because here they have written it as setFieldValue:
> 
> [https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-value](https://dev.wix.com/docs/velo/api-reference/wix-dataset/dataset/set-field-value)
> 
> Is that only ment for a single element and one database field?

It sets a single field in an item, the item is a data set item not the element itself.

If an element is assigned the item from the dataset, it will pick up the change.

@onemoretime did linked above for the setFieldValues() api… which is effectively a shortcut to set more than one field at a time in the item.

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 6, 2024, 3:09pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/12 "2024-11-06T15:09:09Z")

</div>

Okay, thanks for the explaining!

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 7, 2024, 8:40am UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/13 "2024-11-07T08:40:17Z")

</div>

How shall I write “$w(”#dataset5").setFieldValues({" if I don´t wanna connect it with a dataset?

---

<div class="post-metadata">

**Author:** ![Kristofer\_Bath](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@Kristofer\_Bath](https://forum.wixstudio.com/u/Kristofer_Bath)\
**Post date:** [November 10, 2024, 2:57pm UTC](https://forum.wixstudio.com/t/how-can-i-check-a-box-and-update-the-database-with-the-new-value/64248/14 "2024-11-10T14:57:27Z")

</div>

The code above does not work for some reason I can´t understand. I have also tried with this version, both with and without a dataset but the result is always dissapointing 🙁 I beg you to look for faults in it and tell me what I do wrong?

wixData.get(“Members”, userId)  
$w(‘#dataset1’).getCurrentItem()  
.then(()=\>{  
$w(‘#engButton’).onClick((event) =\> {  
let toUpdate1 = {  
\_id: userId,  
english: true,  
spanish: false,  
german: false,  
french: false,  
swedish: false,  
arabic: false,  
language: “english”,  
};  
wixData  
.bulkUpdate(“Member”, [toUpdate1])  
.then((results) =\> {  
let inserted = results.inserted;  
let updated = results.updated;  
$w(‘#dataset1’).save()  
})  
.catch((err) =\> {  
let errorMsg = err;  
});  
setTimeout(() =\> {  
wixLocation.to(`/members/account/update/${memberId}`);  
}, 1200);  
});
