0
votes

I am storing content into a database table. One table column is called attrbutes and has a list of values such as (ex: 1, 3, 5) based on the checkboxes that were checked.

<form>
<input type="checkbox" name="attribute" value="1">Attr 1<br>
<input type="checkbox" name="attribute" value="2">Attr 2<br>
<input type="checkbox" name="attribute" value="3">Attr 3<br>
<input type="checkbox" name="attribute" value="4">Attr 4<br>
<input type="checkbox" name="attribute" value="5">Attr 5<br>
<form>

Couple of questions on how to integrate checkboxes with PHP...

1) How do I check to see if at least 1 checkbox is checked on form submit?

2) How do I turn the checked checkboxes into a list like 1, 3, 5 if checkboxes 1, 3, and 5 are selected.

3) As a reverse to #2, on page load I need to figure out how to check each checkbox that's value is listed in the database column. if 1, 3, 5 is listed in table column, I need checkboxes 1 3 and 5 checked on page load.

I know how to code the basic queries for inserting, updating, and removing etc...but I've never worked with checkboxes and storing values from checkboxes using php before.

2

2 Answers

1
votes

Change you html:

<input type="checkbox" name="attribute[]" value="1">Attr 1<br>
<input type="checkbox" name="attribute[]" value="2">Attr 2<br>
<input type="checkbox" name="attribute[]" value="3">Attr 3<br>
<input type="checkbox" name="attribute[]" value="4">Attr 4<br>
<input type="checkbox" name="attribute[]" value="5">Attr 5<br>

1)

$checkedAttr = $_POST['attribute'];
if(count($checkedAttr) > 0)
    echo "At least one checkbox is selected";

2)

$checkboxList = implode(',', $checkedAttr);

3)

$checkedAttr = explode(',', $yourQueryResultStringContainingTheCheckedList);
<input type="checkbox" name="attribute[]" value="1" <?php if(in_array('1', $checkedAttr)) echo 'checked=\"checked\"'; ?>Attr 1<br>
...
1
votes

1, 2) You can treat form elements as arrays with name="attribute[]" then loop through the posted values in your php as an array.

For example:

<?php
$attributes = $_POST['attribute'];
if(empty($attributes)) {
  echo "No attributes selected";
} else {
  // echo whole array
  print_r($attributes);
  // loop through array
  foreach($attributes as $attribute) {
    echo $attribute." ";
  }
  // create list as one whole string
  $list = implode(',', $attributes);
}
?>

3) When you are building the form (using php) you can check each value in a loop. Note that I also made your labels proper labels so they will also activate the checkbox if clicked.

<?php

// need some code to get db values into array

$attributes = array(1,3,5); // your list

// loop through the amount of checkboxes you want
for($i=1; $i <= 5; $i++) {
    if(in_array($i, $attributs) { // check for a match with current checkbox
      $checked = " checked";
    } else {
      $checked = "";
    }
  echo'<input type="checkbox" name="attribute[]" id="attribute'.$i.'" value="'.$i.'"'.$checked.'><label for="attribute'.$i.'">Attr 1</label><br>'
}
?>