Reputation: 121
im trying to store the values in database, all the values are getting dtored in the database but the issue is with dropdown. when i try to upload the multiselect dropdown only the last selected value is getting stored, where as i want to store all the selected values in the database, can some one guide me how can i do that
//----controller-----
public function portfolio1()
{
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
$this->form_validation->set_rules('type', 'Type', 'required');
if ($this->form_validation->run()==TRUE)
{
$config['upload_path'] = FCPATH.'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['type'] = $this->input->post('type');
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->portfolio1($data);
//Redirecting to success page
redirect(site_url('Home/portfolio1'));
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error);die;
}
}
else
{
$this->load->view('portfolio1');
}
}
//----model----------
function portfolio1($data)
{
//saving records
$this->db->insert('portfolio1', $data);
}
//------view page-----
<?php echo form_open_multipart('Home/portfolio1'); ?>
<div style="padding-top:10%; padding-left:30%">
<div>
<textarea name="first_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("first_content");?></span>
</div><br>
<div>
<textarea name="second_content" rows="4" cols="50"></textarea>
<span><?php echo form_error("second_content");?></span>
</div><br>
<div>
<select name="type" multiple>
<option value="*">All Works</option>
<option value=".bootstrap">Creative</option>
<option value=".html">Photography</option>
<option value=".wordpress">Web Development</option>
</select>
<span><?php echo form_error("type");?></span>
</div><br>
<div>
<input type="file" name="filename">
<span><?php echo form_error("filename");?></span>
</div><br>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
Upvotes: 1
Views: 700
Reputation: 366
I've also had this problem before but I had a work around on the server side.
Normally you get your array values, which in your case, in $_POST['type'], right? So what you wanna do is do it like this...
$type = implode(',', $_POST['type']);
And save it to your database as a comma separated values. And each time you display those fields in your form you will convert them back to an array form and loop through each one of them so that they become selected by default.
$type = explode(',', $_POST['type']);
I'll just use the $_POST variable for uniformity but I guess you already get the idea. Hope this helps!
Upvotes: 1
Reputation: 141
Change your multiselect part like this.
<select name="type[]" multiple>
<option value="*">All Works</option>
<option value=".bootstrap">Creative</option>
<option value=".html">Photography</option>
<option value=".wordpress">Web Development</option>
</select>
I have change the name from type
to type[]
so you will get all selected option.
UPDATE
if ( $this->upload->do_upload('filename') )
{
//print_r($this->upload->data());die;
$data['first_content'] = $this->input->post('first_content');
$data['second_content'] = $this->input->post('second_content');
$data['type'] = implode(",",$this->input->post('type'));
$data['filename'] = $this->upload->data('file_name');
//Transfering data to Model
$this->Contact_model->portfolio1($data);
//Redirecting to success page
redirect(site_url('Home/portfolio1'));
}
Now it will save type
as comma separated in your table.
Upvotes: 1
Reputation: 328
If your dropdown box is multiselect then you have to use array in name of select attribute.
<select name="type[]" multiple>
You will get all selected value in post variable like
<?Php $_POST["type"]; ?>
Upvotes: 1
Reputation: 187
all the values are getting dtored in the database but the issue is with dropdown. when i try to upload the multiselect dropdown only the last selected value is getting stored
Can you please mention in which name your multiselect is posted, and I can't find your view page to check how your attributes in mentioned in dropdown
Upvotes: 1