Setting input field value in code doesn't post value to database

I have a hidden text field, whose initial value I have set to “init” for the purposes of this demonstration. The text field is bound to a field “metadata” in my database.
Now I create a visible checkbox. The code behind the checkbox is:

**if**  ($w('#mycheckbox').checked) 
        $w('#metadata').value = 'checked'; 
 **else** 
        $w('#metadata').value = 'not checked'; 

I then run the “Preview”. I click the checkbox, then submit the form. Result in database: metadata = “init”.
I have used the developer tools in the browser to verify that my code is being run. I even made the text field visible so I can see the value change as I click the checkbox. But the value saved to the database is always “init”.
What am I doing wrong?

import wixData from ‘wix-data’;

// …

if ($w(‘#mycheckbox’).checked){

//we insert some text in the collection saying that the box was checked
let toInsert = {
“metaData”: “Checked”,
};

wixData.insert(“myCollection”, toInsert)

} else {

//we insert some text in the collection saying that the box was NOT checked

let toInsert = {
“metaData”: “Not Checked”,
};

wixData.insert(“myCollection”, toInsert)

}

Wow, that’s super complicated for a simple action.
Why doesn’t setting the value work as expected?
What is “myCollection”? Is that an actual well-known value, or is that something I have to set up elsewhere? And is “metaData” the name of my control on the form, or the name of my field in the database?

@shaulbehr

“myCollection” is the name of your database

“metadata” is the name of the field key in your database

@mikemoynihan99 What if I don’t want to post it yet? I just want to set the value of the text input for now, and post to the database when the user clicks the “Submit” button.

@shaulbehr

you just add an event for when the submit button is pressed…

import wixData from ‘wix-data’;

// …

$w(‘#submitButton’).onClick( function () {

if ($w(‘#mycheckbox’).checked){

//we insert some text in the collection saying that the box was checked

let toInsert = {
“metaData”: “Checked”,
};

wixData.insert(“myCollection”, toInsert)

} else {

//we insert some text in the collection saying that the box was NOT checked

let toInsert = {
“metaData”: “Not Checked”,
};

wixData.insert(“myCollection”, toInsert)

}
})

@mikemoynihan99 OK, but let’s say this is a form with 30 other fields. All the other fields are bound to the database using the nice Wix UI. Just this Metadata field needs to be set in code-behind. But I want everything to save to the same record. How do I do that without having to put every single field into the toInsert object?

@shaulbehr
you spend the 3 minutes writing up the code…

import wixData from ‘wix-data’;

// …

$w(‘#submitButton’).onClick( function () {

if ($w(‘#mycheckbox’).checked){

//we insert some text in the collection saying that the box was checked

let toInsert = {
“metaData”: “Checked”,
“fieldKey2”: $w(‘#Input2’).value,
“fieldKey3”: $w(‘#Input3’).value,
“fieldKey4”: $w(‘#Input4’).value,
“fieldKey5”: $w(‘#Input5’).value,
“fieldKey6”: $w(‘#Input6’).value,
“fieldKey7”: $w(‘#Input7’).value,
“fieldKey8”: $w(‘#Input8’).value,
“fieldKey9”: $w(‘#Input9’).value,
“fieldKey10”: $w(‘#Input10’).value,
"fieldKey11: $w(‘#Input11’).value,
“fieldKey12”: $w(‘#Input12’).value,
“fieldKey13”: $w(‘#Input13’).value,
“fieldKey14”: $w(‘#Input14’).value,
“fieldKey15”: $w(‘#Input15’).value

};

wixData.insert(“myCollection”, toInsert)

} else {

//we insert some text in the collection saying that the box was NOT checked

let toInsert = {
“metaData”: “Not Checked”,
“fieldKey2”: $w(‘#Input2’).value,
“fieldKey3”: $w(‘#Input3’).value,
“fieldKey4”: $w(‘#Input4’).value,
“fieldKey5”: $w(‘#Input5’).value,
“fieldKey6”: $w(‘#Input6’).value,
“fieldKey7”: $w(‘#Input7’).value,
“fieldKey8”: $w(‘#Input8’).value,
“fieldKey9”: $w(‘#Input9’).value,
“fieldKey10”: $w(‘#Input10’).value,
"fieldKey11: $w(‘#Input11’).value,
“fieldKey12”: $w(‘#Input12’).value,
“fieldKey13”: $w(‘#Input13’).value,
“fieldKey14”: $w(‘#Input14’).value,
“fieldKey15”: $w(‘#Input15’).value

};

wixData.insert(“myCollection”, toInsert)

}
})

@mikemoynihan99

@shaulbehr

if you want go the lazy way about it you could use an onChnage event for your checkbox then just set the hidden text on your page to checked or not checked…

export function mycheckbox_change(event) {

if ($w(’ #mycheckbox ‘).checked)
$w(’ #metadata ').text= ‘checked’;

else $w(’ #metadata ').text= ‘not checked’;
}

@mikemoynihan99 Does .text work? I’ve been using .value the whole time.

And, of course, that’s the original problem statement: setting .value does not work w.r.t. having that value saved to the database when you click the “Submit” button.

@shaulbehr .value does not exist for text so you have to use .text if you want to set the text.

Trying to do the same with a text input, and it doesn’t work!

if ($w('#radioGroup1').value === "Gratuit") {
        $w("#input4").value = "GRATUIT";
        $w("#input4").hide();

    }