0

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.

Here is the form enter image description here

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

  • It will not be sent when unchecked, you need to add an input hidden before the checkbox with a false value (or off in your case) – Lk77 Nov 17 '22 at 15:19
  • "You notice in the picture that checkbox" - no, there's no checkbox in the image. Can you share more details, like the code involved? – Nico Haase Nov 17 '22 at 15:23
  • Maybe https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes could help? – Nico Haase Nov 17 '22 at 15:23
  • @Lk77 Please give me more details, why should i put another hidden input? How to set the value of this hidden based on the checkbox that i'm interested in? – Andrei Tudor Nov 17 '22 at 15:24
  • "why should i put another hidden input" - as you've written: you want to be able to read the "off" value of your checkbox, and the hidden input will help achieve that – Nico Haase Nov 17 '22 at 15:25
  • 2
    In this case, all your input array should have index number. The number represent row number. So, when you submit the form it will be [1] => on, [2] => on but `[0]` will be omitted. The HTML attribute should be `name="description[0]"` not just `name="description[]"` - for example. – vee Nov 17 '22 at 15:26
  • Or you can use the `value` attribute like ` – RiggsFolly Nov 17 '22 at 15:34
  • @vee OK, that might solve it, but it's a little more complicated because I don't know exactly what is the final number of rows i will have, since it's a form where I can add unlimited number of rows. (as you can see in the first picture I have an add row button), with jquery. Anyways, it seems that https://stackoverflow.com/questions/1809494/post-unchecked-html-checkboxes helped me. By putting another hidden input with a default value and the same name, i can take it's off value. However, I don't know why i have anextra input value with an "off" value after i post the form. Anyway, thank you! – Andrei Tudor Nov 17 '22 at 15:36
  • @AndreiTudor checkboxes values are only sent when they are checked, that's why an hidden input with the same name is needed if you want to receive a value when the checkbox is unchecked. The value of the input hidden is hardcoded and will be 'off' in your case. When the checkbox is checked, it will override the input hidden value with it's own value ('on' in your case) – Lk77 Nov 17 '22 at 15:37
  • @Lk77 So if you put something useful in the `value` then you will know which one is being sent. – RiggsFolly Nov 17 '22 at 15:43
  • Well you don't need to know which one, they share the same name, what you want is the value, either 'on' or 'off'. If you get 'off' as a value that means the input hidden was submitted but that does not really matter to you on the php side. – Lk77 Nov 17 '22 at 15:44
  • 1
    @AndreiTudor https://jsfiddle.net/pn8vo3qx/ I hope you get an idea from this. – vee Nov 17 '22 at 15:51
  • The solution with the hidden input will not work, because when the checkbox is checked, you will get too much values (one for the hidden and one for checkbox). The only solution I see is the suggestion of @vee with the index number. – stefket Nov 17 '22 at 15:59
  • @stefket "too much values (one for the hidden and one for checkbox)" - no, not if you add the indexes to the markup explicitly by using `name="switch[1]"` – Nico Haase Nov 17 '22 at 16:02
  • @NicoHaase Yes, that's what I said. I suggested the solution with adding indexes to the markup. – stefket Nov 17 '22 at 16:04
  • @stefket You are exactly right. I dealt with it until now, because i didn't know why i got too many off values. I implemented yours (and also @vee) solution. Now it works ok! Without the hidden inputs it's not working. So the given solution for the problem is partially but not entirely right. Thank you so much! – Andrei Tudor Nov 17 '22 at 16:12

0 Answers0