1

Have some checkboxes, which I need to validate into format with '0' (unchecked) and '1' (checked).

<input type="checkbox" name="subcategory1[]" >Text 1<br>
<input type="checkbox" name="subcategory1[]" >Text 2<br>
<input type="checkbox" name="subcategory1[]" >Text 3<br>

I tried validate it with this code (found on internet):

$sc1 = "";
for($i=0; $i<=2; $i++)
  {
  if(isset($_POST['subcategory1'][$i])){ $sc1 .= '1'; }
  else { $sc1 .= '0'; }   
  }

Firstly that works well, but when I add more checkboxes, this isn't working. It set all "1" at the begining of $sc1. Count them well.

Could be problem that they are in 3 divs (one div for one column), but still with same name?

Edit1: $sc1 chould looks like 010

4 Answers 4

1

it's because you're using $sc1 .= in the loop. you're adding your values. so once it' set to 1, any 0 will just be added. get rid of the full stops in your loop (.= should just be =)

update: you should put the keys in the array

<input type="checkbox" name="subcategory1[1]" >Text 1<br>
<input type="checkbox" name="subcategory1[2]" >Text 2<br>
<input type="checkbox" name="subcategory1[3]" >Text 3<br>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply, but $sc1 should looks like f.e. 001101 at the end (it looks like 111000)
ok, its hard to tell it eactly whats going on and exactly what you want without the full script. you should put the nuumbers in the array keys: <input type="checkbox" name="subcategory1[1]" >Text 1<br> <input type="checkbox" name="subcategory1[2]" >Text 2<br> <input type="checkbox" name="subcategory1[3]" >Text 3<br>
0

Change

for($i=0; $i<=2; $i++)

to

for($i=0; $i<count($_POST['subcategory1']); $i++)

If you add more checkboxes you need to loop all of them and not just 3 like you are doing.

1 Comment

thanks for reply, I raised 2 to number i need, this is only example, with only 3 checkboxes
0

It may be because your loop is not running for all the variable. use count() to count the total number of $_POST['subcategory1'] in the for loop or use foreach loop.

1 Comment

thanks for reply, I raised 2 to number i need, this is only example, with only 3 checkboxes
0

There are a few changes you should make.

Add a value to each checkbox so you know which checkbox was submitted by the form posting:

<input type="checkbox" name="subcategory1[]" value='1'>Text 1<br>
<input type="checkbox" name="subcategory1[]" value='2'>Text 2<br>
<input type="checkbox" name="subcategory1[]" value='3'>Text 3<br>

Next loop through each $_POST['subcategory1'] to see which checkboxes were sent in the form post data:

$checked = []; // start with empty array
for ($x = 0; $x < count($_POST['subcategory1']); ++$x) {
  $checked[] = $_POST['subcategory1'][$x]; // add value to array
}

The reason for this change is: if a checkbox is not checked, it does not get sent to the server when the form is submitted. This will build you an array with the values of the checkboxes that were checked.

For example if I checked the Text 1 and Text 3 Checkboxes, the dump of $checked after the for loop would show the value of the attribute named "value":

array (size=2)
  0 => string '1' (length=1)
  1 => string '3' (length=1)

1 Comment

Thanks, and if i need string f.e. 000110, how to edit code, that it "accept" unchacked Checkboxes (or just added "0")?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.