Can a switch have a value of 1 when checked and 0 when unchecked?

Is it possible to setup a switch control to have a value of 1 when checked and 0 when unchecked?

I want to have a variable update every time on value change of the switch to control some other actions on the page.

Checkboxes don’t send any value on form submit when unchecked.
Use the default() formatter for your POST variable in the insert/update record and set 0 there. This way 0 will be stored when no value is sent from the checkbox. When the checkbox is checked, it will send the value it has set: value="1"

EDIT: sorry, i misread your request, thinking you want to submit the value to the db.

Yes.

Add a variable to the page.

In the Dynamic events -> Value changed event, set the value of the variable accordingly.

Set the other actions to the requisite value of the variable you’ve defined.

1 Like

I also submit the value so if user returns later it toggles the switch according to the saved db value.

The default 0 works but I cannot get switch to send a 0 value. IS this because I do not use a form POST and a GET and trigger the form on value changed?

this is my current code for the switch:

<input class="form-check-input" type="checkbox" id="faqtoggle" name="faqtoggle" dmx-on:changed="serverconnectUpdateFAQShow.load({faq: faqtoggle.value})" dmx-bind:value="serverconnectUserDetails.data.queryUser.showFAQ">

The toggle is just a styled checkbox :slight_smile:
How do you send its value to the database then?

yes, I saw it is just styled. Just can’t get it to update the db :slight_smile:

I have this server update action with a $GET var for the checkbox value:

And then on the page I have this server action:

<dmx-serverconnect id="serverconnectUpdateFAQShow" url="/api/MyProfile/updateFAQ" noload dmx-param:faq="faqtoggle.value"></dmx-serverconnect>

and you can see on the checkbox I call the action on changed event

In the database update step apply the default formatter to the GET value :slight_smile:

ok, will do. Just to make sure… the checkbox sends through a value == 1 by default if checked?

The checkbox will send a value 1 if you add a value to it :slight_smile: value="1"

if you add the value = 1 it will always start off as checked? And I want it to use the db value as value, sometimes 0 and sometimes 1, depending on the query

No, the checked state has nothing to do with the value :slight_smile:

like this:

1 Like

Yes, that’s correct.

I have now tried everything I can and I can update a variable, db field and even another text field on change on updated and I get it to change from nothing or 0 to 1 but not back to 0 when toggled off… is there an example of this somewhere please?

What exactly you cannot change to 0?

the db field I want to update, a variable on the page or an input field. I tried all three and once I set value=1 on the checkbox that is all it stays.

So I want to toggle switch on and a value of 1 is either updated on a db field on toggle or set the value of the variable and based on that I can update db or set value of and hidden input field and with that I can also update the db.

Should I put the toggle in a form with post or is it ok to just keep it as a single input field with no form?

So then just use the onchanged event of the checkbox…

dmx-on:changed="var1.setValue(checked ? 1 : 0)"

where var1 is the name of the var you want to change.

1 Like

thank you. not seen this way of doing it before. Can you please explain what (checked ? 1 : 0) means?

Appreciate the help

that’s ternary operator … condition ? true value : false value

if checkbox is checked ? set value to 1 : otherwise set value to 0

2 Likes