I'm new to codeigniter and I'm really confused on how to add options to the drop down list and save the added options in the drop down list to the database. Can anyone help me with this?
My View:
<!doctype html>
<html>
<head>
<title>My Form</title>
<script src="<?php echo base_url();?>assets/js/jquery-1.11.1.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('#myselect').change(function(e) {
if ($(this).val() == "addother") {
$('#addother').show();
$('#addother_input').val('');
} else {
$('#addother').hide();
}
});
$('#add').click(function() {
$.ajax({
type: "POST",
url: window.location,// use ci url here(i.e call controller)
data: { selectboxvalue: $options.val() } ///value will be sent
})
.done(function( msg ) {
$('#add').html(msg);
});
});
});
</script>
<style>
#addother {
display: none;
}
</style>
</head>
<body>
<div id="container">
<?php
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
'addother' => "Add other..."
);
echo form_dropdown('shirts', $options, null, 'id="myselect"');
?>
<div id="addother">
<?php echo form_input(array('id'=>'addother_input', 'name'=>'add', 'placeholder'=>'Enter name of school...')); ?>
<input type="submit" id="add" name="submit" value="+" />
</div>
</div>
</body>
</html>
My Controller:
function add() {
$this->load->model('fruit_model');
$data['selectboxvalue'] = $this->input->post('selectboxvalue');
$res = $this->model->addItem($data);
$this->load->view('myform');
}
function drop() {
$this->load->model('getAll');
$data["opt"] = $this->fruit_model->getAll();
$this->load->view('myform');
}
My Model:
function getAll() {
$query = $this->db->get('fruits')
return $query->result();
}
function addItem($data){
$this->db->insert('fruits', $data);
return;
}
How to save the added options into the database?