Question

I'm making a "working hours" database where users can record their working hours every day. Currently, the form only let's them add one record (day) at a time.

Some users want to do this by the end of the month, and register all days of the month at once. And it would be much easier if the form had a field for every day on one page, and then submit all at once.

What is the recommended way of handling this?

Should I just add 31 rows with fields and name them blabla1, blabla2, blabla3 etc. And then in PHP check one by one, if the field is not empty, and if it's not, do a INSERT? Or are there more elegant ways to solve this?

Currently the DB have these fields: WorkHourID Employee Project StartTime EndTime

Was it helpful?

Solution

It's quite simple, really. Create a HTML like this:

<input type="text" name="hours[]" />
<input type="text" name="hours[]" />
<input type="text" name="hours[]" />

Anything that they submit creates a PHP array like this:

$_POST['hours'] = array([0] => 'value 1', [1] => 'value 2');

I think you can go from there, right? Or do you need any more help?

If you need more of the PHP part:

<?php

foreach($_POST['hours'] as $value){
   if ($value){ // it's not empty
      // maybe do a little more check on whether $value is valid
      // then insert $value to DB
   }
}

?>

OTHER TIPS

In the form you can add a checkbox with label "For the whole month". If the checkbox is ticked then add records of the same total as the days of the month. But add a comment somewhere that if a date is already filled that the corresponding record will not be overwritten. Or you may add another checkbox to overwritte existing records for the month.

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top