Home » Php » php – radio button value inserts incorrectly

php – radio button value inserts incorrectly

Posted by: admin February 25, 2020 Leave a comment

Questions:

Sometimes the radio button value does not insert correctly – I use Codeigniter framework

The radio button value yes/no is not inserted correctly. Sometimes it inserts more than selected “Yes” value or sometimes more “No” value. I couldn’t find the error.

Here is my Controller

    public function save_attendance()
        {
                $std_id=$this->input->post('sid');
                $fac_id=$this->input->post('fac_id');
                $dept_id=$this->input->post('dept_id');
                $semester=$this->input->post('semester');
                $ad_year=$this->input->post('ad_year');
                $subject=$this->input->post('subject');
                $sub_type=$this->input->post('sub_type');
                $class_topic=$this->input->post('class_topic');
                $paper_code=$this->input->post('paper_code');
                $date=date("Y-m-d");
                $time=date("g:ia");

                $ctr=count($std_id);
                for($i=0; $i<$ctr; $i++)
                {
                    $sx=$std_id[$i];
                    $ax=$this->input->post('attendance'.$sx);

                    if ($dept_id=="1") {
                        $this->db->insert('attendance_data_1', array('std_id'=>$sx,'fac_id'=>$fac_id,'dept_id'=>$dept_id,'attendance'=>$ax,'semester'=>$semester,'ad_year'=>$ad_year,'class_topic'=>$class_topic,'subject'=>$subject,'sub_type'=>$sub_type,'paper_code'=>$paper_code,'adate'=>$date,'atime'=>$time));
                    }
                    if ($dept_id=="2") {
                        $this->db->insert('attendance_data_2', array('std_id'=>$sx,'fac_id'=>$fac_id,'dept_id'=>$dept_id,'attendance'=>$ax,'semester'=>$semester,'ad_year'=>$ad_year,'class_topic'=>$class_topic,'subject'=>$subject,'sub_type'=>$sub_type,'paper_code'=>$paper_code,'adate'=>$date,'atime'=>$time));
                    }
                    if ($dept_id=="3") {
                        $this->db->insert('attendance_data_3', array('std_id'=>$sx,'fac_id'=>$fac_id,'dept_id'=>$dept_id,'attendance'=>$ax,'semester'=>$semester,'ad_year'=>$ad_year,'class_topic'=>$class_topic,'subject'=>$subject,'sub_type'=>$sub_type,'paper_code'=>$paper_code,'adate'=>$date,'atime'=>$time));
                    }
                }
                    $this->session->set_flashdata('success', 'Success');
                        redirect('dashboard');
        }

My Views


                <?php $sl=0; foreach($records as $r){ $sl++; $sx=$r->std_id;?>
                    <input type="hidden" name="sid[]" value="<?php echo $r->std_id;?>">
                    <input type="hidden" name="fac_id" value="<?php echo $fac_id?>">
                    <input type="hidden" name="dept_id" value="<?php echo $dpt;?>">
                    <input type="hidden" name="semester" value="<?php echo $sem=$r->semester;?>">
                    <input type="hidden" name="ad_year" value="<?php echo $r->ad_year;?>">
                    <input type="hidden" name="subject" value="<?php echo $sub;?>">
                    <input type="hidden" name="sub_type" value="<?php echo $type;?>">
                  <tr>
                    <td>
                        <?php echo ucwords(strtolower($r->name));?>
                    </td>
                    <td>
                        <?php echo $r->semester;?> - <b><?php echo $r->std_id;?></b>
                    </td>
                    <td>
                        <div class="btn-group" data-toggle="buttons">
                            <label class="btn s_review2 active">
                                <input type="radio" class="custom-control-input" name="attendance<?php echo $sx;?>" value="Yes" id="<?php echo $sx;?>" checked="checked">Yes
                            </label>
                            <label class="btn s_review">
                                <input type="radio" class="custom-control-input" name="attendance<?php echo $sx;?>" value="No" id="<?php echo $sx;?>">No
                            </label>
                        </div>
                    </td>
                  </tr>
                <?php }?>
                    <tr>
                        <td colspan="3"><textarea type="text" placeholder="Enter Class Topic & Details (if any)" required class="form-control form-control-sm" name="class_topic"></textarea></td>
                    </tr>
                    <tr>
                        <td colspan="3">
                            <select type="text" required class="form-control form-control-sm" name="paper_code">
                                <option value="" selected style="display:none;">Select Paper Type</option>
                                <option value="Theory">Theory</option>
                                <option value="Practical">Practical</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="3">
                          <button type="submit" class="btn btn-success btn-sm btn-block">Save Attendance</button>
                        </td>
                    </tr>
How to&Answers: