0

I am building a page containing a number of checkboxes. html is like:

<input type="checkbox" name="gallery[]" value="gal1">
<input type="checkbox" name="gallery[]" value="gal2">
<input type="checkbox" name="gallery[]" value="gal3">

I want to have the name of checkbox (gallery) even if user doesn't choose any item.
so i added a hidden input with the same name(gallery) but in this case even if user select any checkbox , the array received in backend would be :

[
  "gallery" => ""
] 

even if user choose any checkbox item. so this method would not be eligible.
because of the page structure(this page is generating according to user selections in its previous page ) i don't Know what would be the name of checkboxes in this page.also i'm not going to use session. maybe using JQuery solve the issue but i'm trying to avoid that.
so if user doesn't choose any checkbox i want to have:

$_POST : ["gallery" => '']

and if user choose any checkbox then i should have:

$_POST: ["gallery" => ['gal1', 'gal2', ...]]

is there any html base approach to this problem?

alex
  • 7,551
  • 13
  • 48
  • 80
  • I think input name occurs problem – Uttam Kumar Roy Feb 13 '16 at 16:46
  • You already named the checkboxes as *gallery*, what do you mean by *i added a hidden input with the same name(gallery)* ? – Rajdeep Paul Feb 13 '16 at 16:55
  • Can you please elaborate your issue briefly . *Much Needed* – Nana Partykar Feb 13 '16 at 17:07
  • @NanaPartykar if you read the question carefully , i have declated that user may not choose any checkbox and so i won't have `"gallery"` in $_POST in this case , but i aimed to have this item in POST array. i mean if user doesn't choose any checkbox i need to have `["gallery" => '']` in POST request, thats all – alex Feb 13 '16 at 18:20
  • Actually, this was not necessary "*@NanaPartykar if you read the question carefully*" I read your question. I didn't understand. That's why i asked again. – Nana Partykar Feb 13 '16 at 18:38
  • I think there must be a problem on the server side, if the checkboxes are named as you provided here with square brackets and you select one or more of them, then they must be listed in that $_POST array. Check this question for more details: http://stackoverflow.com/questions/4631224/getting-multiple-checkboxes-names-ids-with-php By the way how are these checkboxes generated in that next step? Can you provide more sample code of the process? – emil.c Feb 13 '16 at 18:43
  • 1
    imo, If you want to do this 'hidden checkbox original state;' then: 1) Please do not use the same name. Call the hidden field something like: name="gallery_before". It holds a list of all the 'on and off states'. imo, It is a trivial matter to reconstruct the complete list of checkboxes, and whether they are on or off, from `gallery_before` and the `S_POST['gallery']` checkboxes that are input? – Ryan Vincent Feb 13 '16 at 19:41

0 Answers0