Reputation: 580
I have some sets of radio buttons fetched from the database. Data tables are like -
id title name option1 option2 option3
------ ------- ------- ------- ------- ----------
1 Wi-Fi wifi Yes No (NULL)
4 Parking parking Yes No Optional
I have the following codes to fetch the data from the database.
$get_biz_meta_list = mysqli_query($connect, "SELECT * FROM biz_meta_list")
<form class="form-horizontal" action="add-biz-meta.php" role="form" method="post" >
<?php
while($biz_meta_list = mysqli_fetch_array($get_biz_meta_list,MYSQL_ASSOC)){
?>
<input type="hidden" name="mid" value="<?php echo $biz_meta_list['id'];?>" />
<div class="form-group col-md-12">
<label for="inputEmail1" class="col-md-3 control-label"><?php echo $biz_meta_list['title'];?></label>
<div class="col-md-2">
<input type="radio" name=meta[<?php echo $biz_meta_list['id'];?>] value="<?php echo $biz_meta_list['option1'];?>"/><?php echo $biz_meta_list['option1'];?>
</div>
<div class="col-md-2">
<input type="radio" name=meta[<?php echo $biz_meta_list['id'];?>] value="<?php echo $biz_meta_list['option2'];?>"/><?php echo $biz_meta_list['option2'];?>
</div>
<?php
if($biz_meta_list['option3']!==null){
?>
<div class="col-md-2">
<input type="radio" name=meta[<?php echo $biz_meta_list['id'];?>] value="<?php echo $biz_meta_list['option3'];?>"/><?php echo $biz_meta_list['option3'];?>
</div>
<?php
}
?>
</div>
<?php
}
?>
<div class="form-group">
<div class="col-md-offset-5 col-md-6">
<input type="submit" class="btn btn-primary" value="Save" name="savemeta" />
</div>
</div>
</form>
Then after submitting it will post only selected fields to another table. That means if I select "Yes
" for "Wi-Fi
" then "Title
" and "Option1
" will be saved to the database. If Both "Wi-Fi
" and "parking
" are selected then both the values will be stored in separate row. I tried to catch the value like this-
if(isset($_POST['savemeta'])){
foreach ($_POST['meta'] as $meta){
$meta_name = get_meta_name($meta,$connect);
echo $meta_name;
}
}
$get_meta_name is a custom function that will retrun the title.
But it is showing Blank in the post echo. Please help me.
Upvotes: 0
Views: 1627
Reputation: 3284
You miss some quotes in the radio button name attribute. When you loop over $_POST['meta'], you lose the index which is the ID of your option. I don't know what you get_mena_name function does, but I don't know how it would return a 'name', because $meta just contains 'Yes' or 'No' values.
Correct your radio button quotes
name="meta[<?php echo $biz_meta_list['id'];?>]"
and your php like this:
foreach ($_POST['meta'] as $id=>$value){
//do your stuff with the ID and its value
$meta_name = get_meta_name($id,$connect);
echo $meta_name;
}
Upvotes: 2