SQLite checkboxes how to?

Hello, I am wondering how to store more checkbox datas with checkbox only no submit button or else? So, when checkbox changeing, change the database straight away. No need to push submit button, or anything else. I would like to use sqlite, php only. I know this code is not working and it is wrong, but I share only because this is how long I reached. I tried to figure out, but no luck.
<?php
//Create a new SQLite3 Database
$db = new SQLite3('chckbxs.db');

//Create a new table to our database
$query = "CREATE TABLE IF NOT EXISTS chckbxs (ischecked BOOLEAN)";
$db->exec($query);

//check insert 1
$values['checkboxitem'] = isset($values['checkboxitem']) ? true : false;
if (condition) {
INSERT INTO chckbxs (ischecked) VALUES (1);
}

//DELETE if uncheck

//READ database and make checked which database value is 1
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SQLite</title>
</head>
<body>
<input type="checkbox" name="one" id="one">
<label for="one">One</label>
<input type="checkbox" name="two" id="two">
<label for="two">Two</label>
</body>
</html>
<?php
//Create a new SQLite3 Database
$db = new SQLite3('chckbxs.db');

//Create a new table to our database
$query = "CREATE TABLE IF NOT EXISTS chckbxs (ischecked BOOLEAN)";
$db->exec($query);

//check insert 1
$values['checkboxitem'] = isset($values['checkboxitem']) ? true : false;
if (condition) {
INSERT INTO chckbxs (ischecked) VALUES (1);
}

//DELETE if uncheck

//READ database and make checked which database value is 1
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SQLite</title>
</head>
<body>
<input type="checkbox" name="one" id="one">
<label for="one">One</label>
<input type="checkbox" name="two" id="two">
<label for="two">Two</label>
</body>
</html>
43 Replies
Jochem
Jochem7d ago
you'll need to attach a listener to the change event on the form using javascript that listener then sends a request to the server to actually store it in the database
lanszelot
lanszelotOP7d ago
I know but I have no idea how to
ἔρως
ἔρως7d ago
i wouldnt do that, it's an easy way to ddos your database
Jochem
Jochem7d ago
that's a bit much. Lots of apps store things automatically without having to click save if it becomes an issue, you can cache and save the requests until there have been no changes for a few seconds
ἔρως
ἔρως7d ago
yes, but they dont do inserts and deletes for thst
lanszelot
lanszelotOP7d ago
I do not want to use js
Jochem
Jochem7d ago
then you're SOL this needs JS
ἔρως
ἔρως7d ago
then you cant there is a hacky way, but it is so abysmally bad i dont think i should share
lanszelot
lanszelotOP7d ago
So I coul not use POST with checkboxes? Only with button?
ἔρως
ἔρως7d ago
that question doesnt make sense, im sorry
Jochem
Jochem7d ago
correct you can only trigger a form submit with a submit button, or javascript
lanszelot
lanszelotOP7d ago
I see
Jochem
Jochem7d ago
the alternative non-js way is to not use checkboxes, but links that send you to a PHP page that toggles the state and rerenders the entire page after with the "checkbox" flipped, but that's a Bad Idea™️ Why do you want to avoid Javascript so much?
ἔρως
ἔρως7d ago
i have a hacky awful idea
Jochem
Jochem7d ago
don't 😛
lanszelot
lanszelotOP7d ago
I have no idea how can I work php and js together
Jochem
Jochem7d ago
PHP generates the HTML that's shown in the browser, JS runs in the browser. That's it there's no magic if you need to communicate between the two, you use fetch in JS
ἔρως
ἔρως7d ago
and then you have some url that you send ajax/fetch requests to
lanszelot
lanszelotOP7d ago
yes I know this, but php and js run different time, not together, and I cannot use fetch at all, I cannot understand it. I tried many times to understand
ἔρως
ἔρως7d ago
so?
Jochem
Jochem7d ago
sorry, but it's kind of necessary if you want front and backend to communicate you can't have frontend interactivity communicate with the backend without using something like fetch. XmlHttpRequest works, but it's outdated. You can use libraries like Axios, but they work much the same as fetch submitting forms with JS isn't a practical alternative
lanszelot
lanszelotOP7d ago
yes, I know this is why I try without js
Jochem
Jochem7d ago
that's not a reasonable solution in this situation
lanszelot
lanszelotOP7d ago
I know fetch, I try to learn many times, but something is missing, and I do not understand it
ἔρως
ἔρως7d ago
it's the understanding that php can just be called at any point in time like an api
Jochem
Jochem7d ago
I'd recommend trying again and making a post in #discussions if you get stuck
lanszelot
lanszelotOP7d ago
in this case I cannot solve this. It is too difficult to me.
Jochem
Jochem7d ago
it's honestly a vital skill as a web developer, and not something you can avoid
lanszelot
lanszelotOP7d ago
To me this is a hobby only, so I just cancel my ideas which need databases 🙂 Thank you so much for helping to everyone
ἔρως
ἔρως7d ago
thats how you stop growing as a web developer gotta get out of the comfy zone
lanszelot
lanszelotOP7d ago
yes, but I do not know anyone who can install windows on a computer. Not that who interested about programming. And I have limites where I need some help, but I cannot found.
Jochem
Jochem7d ago
you can make posts in #discussions if you want help learning things
ἔρως
ἔρως7d ago
mdn has examples of this
Jochem
Jochem7d ago
just be specific with your questions, lay out what you know, what you've tried, and how you think things work a "can someone explain fetch" won't get a lot of traction, but if you put some effort into the question I'm sure someone will give it a shot
lanszelot
lanszelotOP7d ago
if someone can ask question that means half understand. I do not undestand at all fatch/api I cannot ask, because I do not know what I do not understand
Jochem
Jochem7d ago
Start with what you do, and go from there 🤷
lanszelot
lanszelotOP7d ago
Bro Code
YouTube
JavaScript Full Course for free 🌐 (2024)
#javascript #tutorial #course ⭐Time Stamps⭐ #1 00:00:00 JavaScript tutorial for beginners 🌐 #2 00:12:32 Variables 📦 #3 00:25:20 Arithmetic operators ➕ #4 00:33:47 Accept user input 💬 #5 00:39:09 Type conversion 💱 #6 00:44:48 Constants 🚫 #7 00:52:30 ⭐ Counter program 🔢 #8 01:01:46 Math object 🧮 #9 01:07:23 Random number genera...
lanszelot
lanszelotOP7d ago
and I reached Promises, and pitch black Promises , Fetch data from an API
Jochem
Jochem7d ago
you practically never write your own promises, so if that's where you got stuck, don't worry about it. Other than that, promises are just code that runs later. You tell javascript "when something happens, please run this code" if you use await, then it's "wait until this happens, then run this code" otherwise it just goes on with whatever's next
lanszelot
lanszelotOP7d ago
I try to use await, but no luck something I do not understand but I have no idea what
Jochem
Jochem7d ago
you can try asking more broad questions too, but be prepared to just get linked articles. If you do your due diligence and read the articles people link, you can always ask follow up questions
lanszelot
lanszelotOP7d ago
do not worry, it is just a hobby. I skip my ideas which need those 🙂 One more time thank you so much for everyone
Jochem
Jochem7d ago
glad to help!

Did you find this page helpful?