Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sharoonamjid's avatar

How to store multiple values of checkboxes in database

I'm trying to store attendance of college students on single submit button, a teacher would mark 'present' or 'absent' checkbox, once all checkboxes are selected I want to store the values of these checkboxes as well as the id's of the student in the database. List of students is coming from the database using foreach() function.

Thank you,

0 likes
13 replies
somnathsah's avatar

If you want to use checkbox then create only one checkbox for present corresponding to each student, if checkbox is checked then student is present and by default it would be absent.

And you can use name for the checkbox like 'student1', 'student2' and so on for the student having Id 1, 2 and so on.

But if you want use present and absent then create radio button instead of checkbox, because same student can not be marked as present and absent (which is possible in case of checkbox) .

YewNork's avatar

You could name your checkbox inputs 'students_present[]' and the value be their id #.

Small example of the inputs

  <input type="checkbox" name="students_present[]" value="1"/>
  <input type="checkbox" name="students_present[]" value="2" />
<input type="checkbox" name="students_present[]" value="3" />

Only the checked boxes will have their value included within the form submission. It should come in as an array so you could do whatever processing you want with that.

//PSEUDO CODE
$students_present = $request->input('students_present');
foreach($students_present as $sp){
Student::is_present_today($sp)
}
1 like
sharoonamjid's avatar

@somnathsah, @YewNork Thank you so much for your answers. I'm still confused, as you suggested that I need to put student id# in the value attribute of checkbox Or radio button, then how could I get the status of absent and present because checkbox or radio button will send the student's id#, not absent or present status.

MaverickChan's avatar

for your case , you can set the value a boolean T or F .

on checked = t not checked = f

then save to your database

MaverickChan's avatar

yes ,

<input type="hidden" value="student's id" name="ids[]">
1 like
sharoonamjid's avatar

and in controller how can I get both values (id and absent/present) and store it in Model say Attendance? Can you please give a short code example?

Thank you so much

MaverickChan's avatar

the name attr is what you will get.

in your controller , after validation the data

$absent = $request->students_present;
$ids = $request->ids;

but there is a problem in your logic , that the id and present data have not match relationship.

My suggestion is , separate student by foreach loop , make checkbox , send data to controller , then save to database

1 like
sharoonamjid's avatar

Sorry I didn't understand what you are trying to say about the logic problem? Please please can you show the short code example which in your mind is a good to be done. I need this to be done very worried about it.

MaverickChan's avatar
Level 47

my bad before

<form method="post" action="...">
@foreach($students as $student)
    <input type="checkbox" name="present[]" value="{{$student->id}}">Absent?
@endforeach 
</form>

this will send all the present students to controller , no absents would be sent.

after you submit(or you can do ajax as you like). and the default value in your database of present is set to be False.

in controller, first always do validation. then

$ids = $request->present;

foreach ($ids as $id) {

    $student = Student::findOrfail($id); // assume you use this model
    $student->present = true;
    $student->save;

}

maybe there is easier way , but this is i can figure now.

1 like
sharoonamjid's avatar

Thank you so much, lets say there are 10 student's, in which 8 student's are present. How can I store the other 2 absent students in Attendance table, because in attendance table there are no other records, there we are going to store all students attendance that whether they are absent or present? The above solution will add only those students who are present.

sharoonamjid's avatar

I need some logic here that will:

First get all batch 12 students from Student table of :

Then in controller I need to do some login which will store 'absent' in Attendance table of those student whose id's are not checked in checkbox.

How can this be done?

MaverickChan's avatar

no need to save to database , just make a where select when you retrieve data.

$students_present = Student::where('present',true)->get();

or for those who are absent:

$students_absent = Student::where('present',false)->get();
1 like

Please or to participate in this conversation.