1

I'm trying to create a Student Attendance web app. Currently have a struct

type Student struct {
    StudentID int
    StudentName string
    Created time.Time
}

All student will be listed on list.html

func ListStudent(w http.ResponseWriter, r *http.Request) {
    students := models.Student.ListAllUser()
    render.HTML(w, http.StatusOK, "list.html", students)
}

list.html contain list of every students follows with Yes and No button for their attendance.

<p>#{{.StudentID}} {{.StudentName}}</p>
<a class="btn" role="button" href="/studentAttend/{{.StudentID}}">Yes</a>
<a class="btn" role="button" href="/studentAttend/{{.StudentID}}">No</a>

I'm struggle what the backend logic process to do with the button to store the students attendance. The idea is to have a request send to the server with Yes for True or No for False for student attendance that day.

The end goal is to have a table with row of students and column of each day of the month. The column day will have a tick for attend the school day and blank for the day absence along the student row.

Example how the table should look like: Image Table

2
  • 1
    The simplest way would be to add ?value=yes and ?value=no to the two html buttons' href attributes. Then your handler for the /studentAttend/ path can retrieve the value query parameter from the request and do its logic based on that. Commented Nov 7, 2022 at 13:32
  • How can I relate the value with the day of the school attend ? Commented Nov 7, 2022 at 16:25

1 Answer 1

0

I think there are multiple ways to achieve your desired solution. The question you should ask yourself is how much effort you want to invest.

One way (a bit more complex) could be, to implement an additional POST endpoint for the attendance of a student. For example:

<form method="POST" action="/studentAttend/:studentID">
    <label for="not_attended">not attended</label>
    <input type="radio" name="attended" id="not_attended" value="false" checked />

    <label for="attended">attended</label>
    <input type="radio" name="attended" id="attended" value="true" />

    <button type="submit">send</button>
</form>

This example may require some javascript for the functionality, e.g. binding the submit event on the state of the radio's, add some AJAX to prevent a page reload, styling the two radios to a toggle switch... and so on.

Another way, as already mentioned in the comments, is to add query parameters /studentAttend/:studentID?value=<yes|no> to your buttons. Also here you could add some javascript to handle the request in the background.


Another thing I would change is the field names of the Student struct:

type Student struct {
    ID      int
    Name    string
    Created time.Time
}

This is a more cleaner way when accessing the fields. Instead of e.g. student.StudentName you have student.Name.

Sign up to request clarification or add additional context in comments.

1 Comment

The thing I am struggling to do is the logic backend after the button are submitted. I have edited original post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.