我的数据库中有一系列城市。有一个编辑按钮,可以显示一个带有表单的模态,他们可以在这里更改城市名称并提交。我很难让city_id从编辑按钮通过模态传递到控制器。我得到一个“消息未定义变量,用于尝试将city_id置于开放代码形式的行上的city_id。
在混音中加入一个模态让我感到困惑。
查看代码
<a href="#" class="btn edit-modal" data-id="<?php echo $c['city_id']; ?>" data-name="<?php echo $c['city_name']; ?>">Edit</a>
<!-- Modal for Edit -->
<div class="modal hide" id="editCityDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">x</button>
<h3>Edit City</h3>
</div>
<div class="modal-body">
<?php $this->load->helper('form'); ?>
<?php echo form_open('/cities/edit_city/'. $city_id); ?>
<label class="control-label" for="name">City</label>
<input type="text" name="city_name" id="city_name" value=""/><br />
<input type="submit" class="btn-small btn-primary" value="Edit City" />
<button class="btn-small" data-dismiss="modal">Cancel</button>
<?php echo form_close(); ?>
</div>
<!-- jQuery for Modal -->
<script>
// scripts for modal windows
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
$('#editCityDialog').modal('show');
});
</script>
控制器代码
// Edit City
public function edit_city() {
$id = $this->uri->segment(3);
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}
型号代码
// Edit City
public function edit_city($city_id, $city_name) {
$data = array(
'city_name' => $city_name
);
$this->db->where('city_id', $city_id);
$this->db->update('cities', $data);
}
答案 0 :(得分:1)
您可以使用已有的city_id
并将操作设置为:
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
//set the forms action to include the city_id
$(".modal-body form").attr('action','/cities/edit_city/'+city_id);
$('#editCityDialog').modal('show');
});
答案 1 :(得分:0)
如果回显<?php echo $c['city_id']; ?>
适用于编辑按钮,则应该为form_open执行此操作,以及
<?php echo form_open('/cities/edit_city/'. $c['city_id']); ?>
我还建议,如果只有edit_city()
才会调用segment(3)
函数,那么这样做会更容易:
// Edit City
public function edit_city($id) {
if ($this->input->city('submit')) {
$city_name = $this->input->xss_clean($this->input->city('city_name'));
$this->cities_model->edit_city($city_id, $city_name);
}
}