php – How do I get input field repopulated?-ThrowExceptions

Exception or error:

I have a form where teachers add scores for students in a class. The problem is after scores are entered and submitted, the input fields do not retain the submitted values.

I want these input fields to keep their respective values so that teachers, on coming back to the form are able to see their previously added scores.

This is my form…

<div class="col-lg-1">
    <label>Test1 </label>
    <input type="hidden" name="session_id[]" value="<?php echo $sessionID; ?>">
    <input type="number" name="mt_ca1[]"  class="form-control input-sm rounded-0" value="<?php echo set_value('mt_ca1[]'); ?>">
</div>
<div class="col-lg-1" id="t2">
    <label>Test2</label>
    <input type="number" name="mt_ca2[]"  class="form-control input-sm rounded-0" value="<?php echo set_value('mt_ca2[]'); ?>">
</div>
<div class="col-lg-1" id="assg">
    <label>Test3</label>
    <input type="number" name="mt_ca3[]"  class="form-control input-sm rounded-0" value="<?php echo set_value('mt_ca3[]'); ?>">
</div>

And the controller…

function assigngradeActionMT()
    {
        for($i=0; $i<count($this->input->post('number')); $i++)
        {

            $data[]=array(
                'section_id' => $this->input->post('section_id')[0],
                'subject_id' => $this->input->post('subject_id'),
                'class_id' => $this->input->post('class_id')[$i],
                'student_id' => $this->input->post('student_id')[$i],
                'session_id' => $this->input->post('session_id')[0],
                'mt_ca1' => $this->input->post('mt_ca1')[$i],
                'mt_ca2' => $this->input->post('mt_ca2')[$i],
                'mt_ca3' => $this->input->post('mt_ca3')[$i],
                'mt_ca4' => $this->input->post('mt_ca4')[$i],
                'mt_project' => $this->input->post('mt_project')[$i],
                'mt_affective' => $this->input->post('mt_affective')[$i],
                'mt_psychomotor' => $this->input->post('mt_psychomotor')[$i],
                'mt_exam'=> $this->input->post('mt_exam')[$i],
                'mt_tot_score'=> $this->input->post('mt_ca1')[$i] + $this->input->post('mt_ca2')[$i] + $this->input->post('mt_ca3')[$i] + $this->input->post('mt_ca4')[$i] + $this->input->post('mt_project')[$i] + $this->input->post('mt_affective')[$i] + $this->input->post('mt_psychomotor')[$i] + $this->input->post('mt_exam')[$i],
            );

        }

        $inserted = $this->mtprimary_model->add1($data);
        if($inserted > 0)
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-success">Grade Added successfully</div>');
            redirect('admin/mtprimary/index');
        }

    }

How to solve:

One way of saving and showing data in the form is to use flash data. To show data only in the next request you can use session flash data like :

$this->session->set_flashdata('item','value');

you can retrieve data using $this->session->flashdata('item');

You can learn more about below URL here:

https://www.tutorialspoint.com/codeigniter/codeigniter_flashdata.htm

Leave a Reply

Your email address will not be published. Required fields are marked *