Instant Database Write on Checkbox Selection

I have a checkbox test_mode which when the user toggles the value, I want the database to be updated immediately, without any submit buttons being pressed.

So to do it I’ve created a form, an update_test_mode Server Action, and I Submit the form onclick of the checkbox being clicked.

It works well, but seems quite a lot of effort for such a simple task.

Did I miss an easier way to do it?

Best wishes,
Antony.

It’s something I do a lot, and I don’t know of any easier way to do it. However, I’ll often copy and paste the form between pages and edit the code manually rather than use the UI. It’s the sort of thing a snippets facility would be useful for.

Thanks for the feedback @TomD! Nice to know I am on the right track…

Yes, copy and paste is the way for now…

… but a parameter based snippet system would be amazing! :slight_smile:

Actually, I’ve thought of another use case…

there will be some times I just want to write to a specific database column when something else happens…

… so do I need an invisible form to be able to submit that, or is it possible to write something to the database by loading a GET type of Server Action? :thinking:

It’s possible but not advisable, as everyone with your server action link can run it in a browser and write to your database.

Hmmm… that’s not good!

But is it the case that if every server action is protected by a Security Restrict then that isn’t possible?

Yes, protecting the file with the security restrict won’t allow this to happen.

Add the “toggle” server action to the page with “No Auto Load”

You can then run a serveraction.load(‘parameters if necessary’) within a click dynamic event of the checkbox

1 Like

Thanks for the idea @Hyperbytes! :slight_smile:

Much appreciated…

I’m currently struggling with this.

Have a check box, Dynamic attribute ‘Checked’ is updated from serverconnect. Works great to show state. easy part :slight_smile:

I want to update the db record on change of state.

  • i set static value to 1
  • added dynamic click event
    -added the server connection action that updates the record (no autoload), assigned the checkbox value to the parameter (which has default value 0 on action)
    -reload the first sever connect to show new state.

Issue:

I am able to write 1 to the database but never 0. with debug enabled i see its always sending param=1 everytime i click the box, never blank. i want it to submit a blank value when not checked.

I’ll keep trying!

thanks.

/J

Hi Johnny,

Try adding a default value of 0 to your server action. That way, if there is no value it should enter the 0, if there is a value it will add the value.

I wish it didn’t send a value :slight_smile: it’s always sending 1 that’s my problem.

That’s why I suggested setting a default value in your server connect action. If it is unchecked it should submit whatever you have for a default value. You can even set your default value to Null if you want no value at all. If it’s checked it will send the 1 value. If it’s unchecked it will set the default value which can be null in your case.

Hello brad,

i understand the purpose of the Default value. if not checked it sends no value and get replaced by default. nonetheless i added default 0 everywhere i could find

My issue is that it always sends ‘1’ its never blank.

There is no submit button or form

I ended up using this method to send value to parameter on click.

it works. when box is unchecked and i click it, value it 1 otherwise 0

Your update action looks right except should it not be POST instead of GET?

That’s an interesting solution. Glad it works for you!

I just tested with POST. now it only sends 0 :slight_smile:
so i’ll stick to my solution for now…

thanks

1 Like