-1

The idea is to have list of checkbox for user to select genre type. The genre input is from database. Because the sql will return the result in an array, a for loop to get all the values and make them into a check box. The error come from third line in the loop. It show undefined index genre

<div class="list-group">
      <h3>Genre</h3>
      <?php
      $search_genre = "SELECT DISTINCT(genre) FROM movie_db WHERE product_status ='1' ORDER BY movieid DESC"
      $statement = mysqli_query($conn,$search_genre);
      $result = mysqli_fetch_all($statement);
      foreach ($result as $row){
         ?>
         <div class="list-group-item checkbox">
         <label><input type="checkbox" class="common_selector genre" value="<?php echo$row['genre'];?> > <?php echo $row['genre'];?></label>
</div>
<?php
}
?>
user9510596
  • 41
  • 1
  • 5

1 Answers1

3

You need to give the checkbox a name, and since you are allowing multiple selections (checkbox vs radio button) you need to use a PHP array style name -

<label>
<input 
name="mycheckbox[]" 
type="checkbox" 
class="common_selector genre" 
value="<?php echo $row['genre'];?>"
<?php // here is where you could throw a check to make it pre-selected
by printing the word "checked" if a logical condition is met, etc ?>
> 
<?php echo $row['genre'];?> 
</label>

Now, in your processing script, you can reference $_POST['mycheckbox'] and it will contain an array with one or more values in it, one element per selected value.

for($i=0;$i<count($_POST['mycheckbox']);$i++){
  print("selected value $i: ".$_POST[['mycheckbox'][$i]."\n");
}

Edit -

Just for giggles, if you wanted only one single selection (radio button) you STILL name the elements the same thing, you just leave off the PHP array brackets []

<label>
<input 
name="myradio" 
type="radio" 
class="common_selector genre" 
value="<?php echo $row['genre'];?>"
<?php /* here is where you could throw a 
      check to make it pre-selected
      by printing the word "selected" 
      if a logical condition is met, etc 
      note in a series the last one selected
      stays selected if you print selected more than once
      */
?>
> 
<?php echo $row['genre'];?> 
</label>

And the selected radio group value is available as $_POST['myradio']

ivanivan
  • 2,155
  • 2
  • 10
  • 11