I have a problem in PHP and I tought maybe someone can help me with this.
So I have a form to post some rows into a database.
To give some context, my form is a table, where each column is an input. Each row is an entry that i want to insert into a table. So the values of each entry are set from the columns.
Now, i put every input name as an array, for example for description:
<td><input class="form-control" name="description[]" type="text" placeholder="Description"></td>
And for this I don't have any problems, because there can't be empty lines regarding this description, every description must be set.
Now the problem:
You notice in the picture that checkbox. The problem is that I cannot take the value of the checkbox for every specific entry, because if it is not set, the $_POST value of it is not set.
So for example, if I have 3 entries like this: (i will put only 2 columns so it's simple to explain):
Description | Amount |
---|---|
123 | checkbox not checked |
456 | checkbox checked |
789 | checkbox checked |
My values in $_POST will be
$_POST['description']:
Array
(
[0] => 123
[1] => 456
[2] => 789
)
$_POST['switch']:
Array
(
[0] => on
[1] => on
)
And the array keys won't correspond between eachother and i cannot associate a description with a state of a switch because of that...
Now is there a way to solve this? Can I have any default value for the switch or something, so that i can compare it's value, not if it was posted or not? For example a default value of switch to "off" to make the result like:
$_POST['switch']:
Array
(
[0] => off
[1] => on
[2] => on
)
Thank you!!
I tried to set the value parameter to the input, but nothing happens