dimanche 20 décembre 2015

Inserting multiple checkbox values with a same text field value (codeigniter)

I'm trying to put multiple checkbox values into a MySQL table with a same text value.

The basic idea is from this pict (selecting value for a same input text value).

Instead of inserting a same "Subject Name" multiple times for each option tag, I'm trying to make it one time submit with checkbox tag. It will be something like this pict (with checkbox).

I know the business will be done in the Controller and Model. I just don't have the idea how to make this happen.

This is some code from my Controller,

public function add_subject()
{
    $data = array();

    if ($this->input->post('savebttn'))
    {
        $this->form_validation->set_rules('sbjct_name', 'Subject Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('level', 'Level', '|required|is_natural');
        $this->form_validation->set_error_delimiters('<span class="fielderror">','</span>');

        if ($this->form_validation->run() == FALSE)
        {
            $data['reset'] = FALSE;
        }
        else
        {
           //I don't know what to do here
           //I don't know what to do here
           //I don't know what to do here
           //I don't know what to do here
        }
    }
    $data['level'] = $this->admin_model->get_checkbox_option('level', 'lvl_id', 'lvl_name');
    $data['page'] = 'createsubject';
    $this->load->view('admin/main', $data);
}

This is a spesific function in the Model (to pass checkbox value to View page),

public function get_checkbox_option($table, $id, $name, $selected=0)
{
    $query = $this->db->get($table);
    $select= '';
    if ($query->num_rows() > 0)
    {
        foreach ($query->result_array() as $row)
        {
            $selected_option = ($selected == $row[$id]) ? 'selected = "selected" ':' ';
            $select .='<input type="checkbox" name="level" value="'.$row[$id].'" '.$selected_option.'>'.$row[$name].'<br>';
        }
    }
    return $select;
}

This is the form from the View,

    <form action="" method="post" id="createcategoryform">
        <table>
            <tbody>
                <tr>
                    <td><div class="label">Subject Name</div></td>
                    <td><div class="input">
                        <input type="text" name="sbjct_name" size="39" class="ui-corner-all input-text validate[required]">
                        <?=form_error('sbjct_name');?>
                    </div></td>
                </tr>
                <tr>
                    <td><div class="label">Level</div></td>
                    <td><div class="input-text ui-corner-all validate[required]">
                        <?=(isset($level)) ? $level: '';?>
                    </div><?=form_error('level');?></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" value="Save" name="savebttn" class="input-button ui-corner-all ui-state-default"></td>
                </tr>
            </tbody>
        </table>
    </form>

Is this possible to do in Codeigniter? I will be grateful if you can help me figure this out. :D




Aucun commentaire:

Enregistrer un commentaire