How to work with dynamic form input number using PHP?

Hi, I have a form where the user can select the start date and end date of the vacation. For example, if the number of days is 3, 3 date lines will be created. Each date, day and period (am / pm) will be stored in a hidden field. Thus, 3 days will create a hidden field with the name date_1, day_1, period_1, date_2, day_2, period_2, date_3, day_3, period_3.

The question is how to handle this dynamic form input number? I need to pass the value to the controller and then simulate it for storage in the database. This is the main problem, since the form input is dynamic, and we need to pass it to the controller function.

Can someone show me the correct way to deal with this problem? A link to the tutorial will be useful thanks :)

This is the code used to create the date list, as in the image below.

function test(){
            var count = 0;
            var date1 = $('#alternatestartdate').val();
            var date2 = $('#alternateenddate').val();
            var startDate = new Date(date1);
            var endDate = new Date(date2);
            var Weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
            while (startDate<=endDate)
            {
            var weekDay = startDate.getDay();
            if (weekDay < 6 && weekDay > 0) {
            var month = startDate.getMonth()+1;
            if( month <= 9 ) { month = "0"+month; }
            var day = startDate.getDate();
            var datearr = new Array();
            if( day <= 9 ) { day = "0"+day; }
            count++;
            var datelist = day+"-"+month+"-"+startDate.getFullYear();
            $('#pa').append(day+"-"+month+"-"+startDate.getFullYear() + " ("+Weekday[weekDay]+") <input type='hidden' id='' name='date_"+count+"' value='"+datelist+"' /><input type='hidden' id='' name='day_"+count+"' value='"+Weekday[weekDay]+"' /><input type='radio' name='period_"+count+"' value='1' checked/>Full<input type='radio' name='period_"+count+"' value='2'/>Half (AM)<input type='radio' name='period_"+count+"' value='3'/>Half (PM)<br />");

            }
            startDate.setDate(startDate.getDate()+1)
            }
            $('#pa').append("<input type='hidden' id='' name='countval' value='"+count+"' />");
        }

alt text

If inserted correctly, the data in the database will look like this:

alt text

+5
source share
1 answer

If you put empty brackets at the end of the name attribute of all related tags, for example:

<input type='hidden' name='blah[]' value='foo' />
<input type='hidden' name='blah[]' value='bar' />

then they will be sent through the array $_POSTas an array.

So, you refer to them as follows:

<?php

echo($_POST['blah'][0]); // foo
echo($_POST['blah'][1]); // bar

?>
+5
source

All Articles