如何将多个HTML表单中的值添加到要发送的一个数组中

问题描述:

I'm building a mobile web application with which employees can submit their worked hours for a given week. The form shows 7 forms (each form represents a day), and is built by a php loop function which runs an array containing information from the week. Each day is a different form.

I cannot combine all days into one, because I need to allow employees to add fields to each day (form in div) themselves to add activities they performed while working.

It looks like this: enter image description here

If i combine all the days into one form, each time you add a field on a certain day, it adds the field to the first day (while you may need it on the last day).

The Question

I need to submit the entire week using one button. I have already seen javascript methods like

document.getElementById("form1").submit();
document.getElementById("form2").submit();

But this does not work properly. Can anyone provide me with a direction or some code which helps me to solve this? Any help would be highly appreciated!

我正在构建一个移动Web应用程序,员工可以使用该应用程序提交给定周的工作时间。 表单显示7个表单(每个表单代表一天),并由php循环函数构建,该函数运行包含本周信息的数组。 每一天都是不同的形式。 p>

我无法将所有日子合并为一个,因为我需要允许员工自己添加每天的字段(div中的表单)以添加他们在工作时执行的活动。 p> \ n

看起来像这样: p> \ n

如果我将所有日期合并到一个表单中,则每次在某一天添加​​字段时,它会将字段添加到第一天(您可能在最后一天需要它)。 p> \ n

问题 strong> p>

我需要使用一个按钮提交整周。 我已经看过javascript方法,如 p>

  document.getElementById(“form1”)。submit(); 
document.getElementById(“form2”)。submit(); 
   code>  pre> 
 
 

但这不能正常工作。 任何人都可以向我提供方向或一些代码,帮助我解决这个问题吗? 任何帮助将非常感谢! p> div>

Can you not just achieve this with one form?

HTML

<form>
    <fieldset>
        <!-- all your fields for the day -->
        <legend>Monday</legend>
    </fieldset>

    <fieldset>
        <!-- all your fields for the day -->
        <legend>Tuesday</legend>
    </fieldset>
    <!-- ... -->

    <input type="submit" />        
</form>

Looking at the tags that you have added to the question, I assume that you use PHP on the server side. In this case I would use arrays for your input field names:

<form method="post">
    <fieldset>
        <legend>Maandag</legend>
        <input type="text" name="days[maandag][verantwoording][klant][]"><br>
        <input type="text" name="days[maandag][verantwoording][klant][]"><br>
        <input type="text" name="days[maandag][verantwoording][klant][]">
    </fieldset>
    <fieldset>
        <legend>Dinsdag</legend>
        <input type="text" name="days[dinsdag][verantwoording][klant][]"><br>
        <input type="text" name="days[dinsdag][verantwoording][klant][]"><br>
        <input type="text" name="days[dinsdag][verantwoording][klant][]">
    </fieldset>
    <button type="submit">
</form>

Then it is easy to access all of the fields in PHP because PHP will add an (zero-based) index to each of the []:

<?php

echo $_POST['days']['maandag']['verantwoording']['klant'][0];
echo $_POST['days']['maandag']['verantwoording']['klant'][1];
echo $_POST['days']['maandag']['verantwoording']['klant'][2];

See also the documentation on HTML forms