Question

I have a calendar that I create dynamically via a php for-loop (each day is a div). On each day there should be the possibility to enter something, like "on holidays" or "away", preferably by way of a checklist. I bascially want a calendar, that shows the entries from a database for each day (that basically works), but also you should be able to say "on the 4th of March I'm out of office" and save that to the database.

I'm struggling with the updating part.

How do I get all of the values from all input fields and update them to the database? What would be the best way? should I wrap all of the inputfields in a form and then go through each form?

No correct solution

OTHER TIPS

The best way is to wrap all your inputs in one form. After that, you need to think about your data. Do you need to distinguish these inputs somehow? Then you need to give unique name to each of them. Otherwise, you can just pass them like an array (examples here).

I would wrap a form around all divs.

And then you can write the input-fields like:

<input type="text" name="info-for-day[03-04][away]">
<input type="text" name="info-for-day[04-04][away]">

So in PHP you can access the value of the field with:

$value1 = $_POST['info-for-day']['03-04']['away'];
$value1 = $_POST['info-for-day']['04-04']['away'];

Now you can write this value with a SQL-Statement in your database, hope that will help. Best solution is a foreach i think:

foreach ($_POST['info-for-day'] as $date => $value) {
    mysqli_query($conn, "Update ... SET field=$value['away'] WHERE date=$date");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top