I’m new to php and html and i need some help for my project. I have seen a few examples online but i don’t quite understand them. What i want to do is to update the database using a form with a checkbox. If it is checked then the database should be updated as 1. Else if it is unchecked then update it with 0. Currently, my issue is not knowing how to make it so that the code will update the database with 1 or 0 if it is checked or unchecked. I know that i have to create a separate php file to do it but i’m not sure how to do it. Below is the code i used for the form. Please help me.
<i class="icon-book text-muted"></i> Select modules to enroll student in:<br><br>
<div class="form-group has-feedback has-feedback-left" style="padding-left:25px">
<div class="checkbox">
<input type="checkbox" name="module[1]" value="1"><label>MRO</label>
</div>
</div>
<div class="form-group has-feedback has-feedback-left" style="padding-left:25px">
<div class="checkbox">
<input type="checkbox" name="module[2]" value="1"><label>CNC</label>
</div>
</div>
<div class="form-group has-feedback has-feedback-left" style="padding-left:25px">
<div class="checkbox">
<input type="checkbox" name="module[3]" value="1"><label>Aero-Science</label>
</div>
</div>
<div class="form-group has-feedback has-feedback-left" style="padding-left:25px">
<div class="checkbox">
<input type="checkbox" name="module[4]" value="1"><label>Aero-Structures</label>
</div>
</div>
<div class="form-group has-feedback has-feedback-left" style="padding-left:25px">
<div class="checkbox">
<input type="checkbox" name="module[5]" value="1"><label>Aero-Systems</label>
</div>
</div>
UPDATE: Now that i am using the insert function. If i select only one of the checkbox, the selected checkbox gets updated in the database. However, i will get an error code of, Undefined index: module_2, if i did not select the second checkbox. In the table in the database, it the unchecked modules appear as empty and not 0. How do I make it so that the unchecked boxes will appear as 0 in the database. I’m using phpMyadmin as the database btw.
3
Answers
First, you need to enclose your code in a form. Like this,
Then in your submit_form.php file get the value like this,
Then using mysql insert query, push your values to db table.
OK for that you have to send AJAX request. So that you can easily update database without refreshing the current page. You can handle it like this-
Note : don’t forget to set id name in input field & jquery CDN.
Hi You can use isset to assign the 0 or the 1
Then use these values in your PDO or your prepared statement and it will update it