Welcome Guest, Not a member yet? Register   Sign In
Getting Multi-select data from a form
#1

[eluser]Bionicjoe[/eluser]
My app has a multi-select field in one form, but when submitted to the db only one item is inserted. Any help is appreciated or point me to a good tutorial that shows creating a multiselect in CI from a db table and then adding the selections to the record table.

View to make the multi-select
Code:
<tr><td>Location                 </td><td>&lt;?php echo form_multiselect('sitename', $siteoptions); ?&gt;</td></tr>

Model to get the multiselect options from the db
Code:
function sitemultiselect()
    {
        $query = $this->db->get('location')->result_array();
        $result = array();
        foreach($query as $q)
         $result[$q['sitename']] = $q['sitename'];

        return $result;
    }

Controllers to add
Code:
function addoutage()
    //This function creates the Add Outage page itself.
  {
    $this->load->helper('form');
    $this->load->model('outage_model','');
    
    // display information for the view
    $data['siteoptions'] = $this->outage_model->sitemultiselect();
        $data['serviceoptions'] = $this->outage_model->servicedropdown();

    $this->load->view('template_view', $data);
  }
  
  function createoutage()
  //This function controls the form to submit the outage to the db.
  {
    $this->load->model('outage_model','',TRUE);
    $this->outage_model->addoutage($_POST);
    redirect('index','refresh');
  }
#2

[eluser]chazy (aldever calvo)[/eluser]
hi.. may i ask how you solved the problem?.. i am having similar problem... but mine doesnt add anything at all.
#3

[eluser]defectivereject[/eluser]
This was my solution. (Obviously going into your model to write the record back to your database!!!)

Code:
$data = array();
//incoming data from multi select
        if($_POST['form_input_name_multiselect']) {
            foreach($_POST['form_input_name_multiselect'] as $val) {
                $values[] = $val;
            }
            
            $data = array(    
            'table_field_name_for_multiselect' => implode(", ", $values),
            'other_table_field_name' => $this->input->post('form_input_name')
//have as many fields as you need here to write back to your db
            );            
            $this->db->insert('table_name', $data);
        }

EDIT!!
Oh and your form multiselect MUST be an array as follows
Code:
echo form_multiselect('input_name[]', $name, '', 'id="input_name"');

//note the [] straight after the field name
#4

[eluser]chazy (aldever calvo)[/eluser]
this was what i did... in this post: http://ellislab.com/forums/viewthread/180856/

i'll try using implode and explode... it'll be my first time to use those functions... Smile
#5

[eluser]chazy (aldever calvo)[/eluser]
thanks... i have the values now, but still not inserted...
Code:
A Database Error Occurred
Error Number:

ERROR: invalid input syntax for integer: "3, 25, 13" LINE 1: ..."ch_act_mat_mat_id", "ch_act_mat_act_id") VALUES ('3, 25, 13... ^

INSERT INTO "activity_materials" ("ch_act_mat_mat_id", "ch_act_mat_act_id") VALUES ('3, 25, 13', '6')

i followed your code and this is my model:
Code:
function AddSetMaterial($actNum, $actMat)
    {
        $data = array();
        if($_POST['actMat']) {
            foreach($_POST['actMat'] as $val) {
                $values[] = $val;
            }
            
            $data = array(
                'ch_act_mat_mat_id' => implode(", ", $values),
                'ch_act_mat_act_id' => $this->input->post('actNum')
            );

            $this->db->insert('choy_activity_materials', $data);
        }
    }

for my controller:
Code:
function setmaterials()
    {    
        $activity = $this->activities_model->get_actnum_dropdown();
        $data['activity'] = $activity;
        
        $material = $this->activities_model->get_mat_dropdown();
        $data['material'] = $material;
        
        //Validate Form
        $this->form_validation->set_error_delimiters('<div class="form_error">','</div>');
        $this->form_validation->set_rules('actNum', 'Activity Number', 'trim|required');
        $this->form_validation->set_rules('actMat[]', 'Material', 'trim|required');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('activities/activities_setmaterials_form', $data);
        }
        
        else
        {
            //The form is validated
            $actNum = $this->input->post('actNum');
            $actMat = $_POST['actMat'];
      
            $this->activities_model->AddSetMaterial($actNum, $actMat);

        }
    }
and for view:
Code:
&lt;?php $actMat= $this->input->post('actMat[]');?&gt;
        <label>*Material :</label>
        &lt;?php echo form_multiselect ('actMat[]', $material, $actMat)
        &lt;?php echo form_error('actMat[]')?&gt;
#6

[eluser]chazy (aldever calvo)[/eluser]
it's good if i changed the datatype of my db form integer to varchar...
i hope theres another way of storing multiple int values because my goal is to insert the integer values per row of the column...
#7

[eluser]defectivereject[/eluser]
try changing this line
Code:
'ch_act_mat_mat_id' => implode(", ", $values),

to this

Code:
'ch_act_mat_mat_id' => implode(",", $values),
All i've done is removed the space in the implode
The only thing i can think of as an INT field would accept any spaces as they aren't numbers
#8

[eluser]chazy (aldever calvo)[/eluser]
done that... the problem doesn't change... the values could not be stored per row... Sad
#9

[eluser]chazy (aldever calvo)[/eluser]
solved in this thread

http://ellislab.com/forums/viewthread/180856/




Theme © iAndrew 2016 - Forum software by © MyBB