【发布时间】:2014-11-27 04:43:43
【问题描述】:
我是 codeigniter 的新手,我对如何将选项添加到下拉列表并将下拉列表中添加的选项保存到数据库感到非常困惑。谁能帮我解决这个问题?
我的观点:
<!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>
我的控制器:
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');
}
我的模特:
function getAll() {
$query = $this->db->get('fruits')
return $query->result();
}
function addItem($data){
$this->db->insert('fruits', $data);
return;
}
如何将添加的选项保存到数据库中?
【问题讨论】:
-
你的意思是你需要在选择框中动态添加值并将其保存在数据库中
-
是的,先生。我真的遇到了麻烦。
-
做一件事,只需使用 ajax 保存并显示 ajax 响应的下拉列表...
-
你有那个先生的例子吗?这样我就可以查看它并了解它的作用。
-
看我的回答。如果你不满意我会编辑它
标签: php jquery database codeigniter drop-down-menu