1

I have code similar to the following:

<input type="checkbox" name="visitProperty" value="1" id="visit-0">
<input type="checkbox" name="visitProperty" value="1" id="visit-1">
<input type="checkbox" name="visitProperty" value="1" id="visit-2">
...

Once the form is submitted I want to get checked checkboxes, so far I've been using

if (isset($_POST['visitProperty']) {..}

But to my understanding it only gets one checkbox? Where as I need to check all of them and see if they were checked, so inside the if statement I can create a loop that gets id's of all submitted checkboxes and then gets the number from id, to update a certain array.

Ilja
  • 44,142
  • 92
  • 275
  • 498
  • name="visitProperty[]" – Dexa Feb 25 '14 at 16:21
  • @Dexa thats the first time I see something like that what does [] do? – Ilja Feb 25 '14 at 16:22
  • it's saying that visitProperty is an array. Also change values to something like 1,2,3... Or just check this answer: http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes – Dexa Feb 25 '14 at 16:23
  • @Dexa alright I see, so is it saying it is an array of inputs? – Ilja Feb 25 '14 at 16:24
  • I believe you have confused checkboxes with radio buttons. Checkboxes must have all unique `name` attributes. Unless of course you want them stored in an array, in that case you say the name attribute to `name="visitProperty[]"`. – Franco Selem Feb 25 '14 at 16:26

3 Answers3

0
<input type="checkbox" name="visitProperty[]" value="1" id="visit-0">
<input type="checkbox" name="visitProperty[]" value="2" id="visit-1">
<input type="checkbox" name="visitProperty[]" value="3" id="visit-2">

<?php
    foreach($_POST['visitProperty'] as $check) {
        echo $check . "<br>"; // for example
    }
?>

NOTE: $_POST['visitProperty'] will hold checked checkbox values. You will access all the checkboxs as an array as following $_POST['visitProperty'][]

nanobash
  • 5,419
  • 7
  • 38
  • 56
  • This is not going to work, checkboxes only get sent to the server when checked, so if not all are checked, you will not be able to identify which ones were checked on the server-side; you'll have an array with some items all with the same value. – jeroen Feb 25 '14 at 16:30
  • @jeroen Updated (Just showed how to avoid empty set of checkbox) – nanobash Feb 25 '14 at 16:32
  • That's not what I mean. If one box gets checked, how will you know whether it was the one with the html ID `visit-0`, `visit-1` or `visit-2`? – jeroen Feb 25 '14 at 16:34
  • It is not supposed to get which checkbox value belongs to html ID with this technique – nanobash Feb 25 '14 at 16:37
  • @jeroen IDs just helps us in case of JavaScript, in case of php why we need to know which ID belongs to which value, the point is to get the values like OP posted – nanobash Feb 25 '14 at 16:39
  • Exactly. But you would need to know which ones were checked, so you need to hard-code some kind of ID in your array. – jeroen Feb 25 '14 at 16:39
  • Yes, if you use unique values like in your latest edit it would as well. – jeroen Feb 25 '14 at 16:40
  • @jeroen We can understand which value belongs to which checkbox with sequence too – nanobash Feb 25 '14 at 16:41
  • @jeroen Yes, at first I just copied the html from OP's post, and then changed the values – nanobash Feb 25 '14 at 16:41
  • Very funny, all values were the same until just a moment ago :-) – jeroen Feb 25 '14 at 16:41
  • @jeroen Yes, in case of usability is was, but the point of this question was just to get the values lol – nanobash Feb 25 '14 at 16:42
0

When you put in the name, you are declaring a variable. You need to declare it as an array, or each checkbox will bump out the last one. Add some empty square brackets to the name.

bennett_an
  • 1,718
  • 1
  • 16
  • 35
0

You would need or defined ID or a unique value, otherwise you will not be able to identify them on the server-side (the ID does not get sent in $_POST).

So in case of a unique, identifyable ID, you could do something like:

<input type="checkbox" name="visitProperty[<?php // echo some unique id from a database for example ?>]" value="1" id="visit-0">

The reason you would need the ID to be identifyable, is that unchecked checkboxes do not get sent to the server, so you might end up with an array of 2 if visitProperty is an array, but you would not know which 2.

jeroen
  • 91,079
  • 21
  • 114
  • 132