Python Forum

Full Version: [FLASK] checkbox onclick event
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Im new to this forum, and already asking my first question ....


I googled a lot, but didnt found a clear answer for that.

Is it (somehow) possbile to add a "onclick" event to a checkbox / slider in flask ?
Just like in javascript ?


The goal should be that i check/uncheck a checkbox on the website, and my databaseUpdate function gets called, without pressing a confirm button, or a refresh of the site.
( in my opinion a confirmation button is intolerable for a user. It simply sucks to " confirm " a slider change / checkbox state change, to say it in rough words ... )

Well, back to the problem.
How could I solve that ?

Does it make sense, to set a href (/ does a onclick work ? )attribute to my checkbox, which redirects to a route, where i just call the update db function ?


I dont really know ...

Would appreciate any kind of help !

Thanks
(May-12-2020, 09:51 AM)Mad0ck Wrote: [ -> ]Does it make sense, to set a href (/ does a onclick work ? )attribute to my checkbox, which redirects to a route, where i just call the update db function ?

This is almost right way to solve the problem. Roughly speaking, the algorithm should be the following:
1. You need to implement a function (in javascript) that will be called when the checkbox is clicked.
2. This function will initiate a request (e.g. XMLHttpRequest) to a specific route;
3. On the backend tail, you need to implement a view that will process such requests (using Python, Flask); To make all of this more secure, you probably will need to use flask-related features, such as sessions and csrf-token handling.
(May-13-2020, 12:32 AM)scidam Wrote: [ -> ]
(May-12-2020, 09:51 AM)Mad0ck Wrote: [ -> ]Does it make sense, to set a href (/ does a onclick work ? )attribute to my checkbox, which redirects to a route, where i just call the update db function ?

This is almost right way to solve the problem. Roughly speaking, the algorithm should be the following:
1. You need to implement a function (in javascript) that will be called when the checkbox is clicked.
2. This function will initiate a request (e.g. XMLHttpRequest) to a specific route;
3. On the backend tail, you need to implement a view that will process such requests (using Python, Flask); To make all of this more secure, you probably will need to use flask-related features, such as sessions and csrf-token handling.


Thanks for the fast reply !