I have a list of cities in my database. There is an edit button that brings up a modal with a form where they can change the name of the city and submit. I’m having a hard time getting the city_id to pass from the edit button through the modal and to the controller. I get a “message undefined variable for the city_id on the line that tries to put the city_id in the form open code.
Adding a modal into the mix has made this confusing to me.
View Code
<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>
Controller Code
// 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);
}
}
Model Code
// 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);
}
You can use the
city_idyou already have and set the action with that: