我有一个包含3 input type="text
和1 select option
的编辑表单。当我尝试更新到数据库表时,input text
的值正常工作。但我遇到了select
选项的问题。
我在html data-*
中使用button
属性:
<?php foreach ($driver as $d) : ?>
<button class="btn btn-warning edit_button"
data-toggle="modal"
data-target="#edit_driver_modal"
data-id="<?php echo $d['id'];?>"
data-email="<?php echo $d['email'];?>"
data-name="<?php echo $d['name'];?>"
data-phone="<?php echo $d['phone'];?>"
data-locationid="<?php echo $d['location_id'];?>"
data-location="<?php echo $d['location_name'];?>">
<span class="glyphicon glyphicon-pencil"></span></button>
<?php endforeach ; ?>
模态内的Edit
形式:
<form method="post" action="<?php echo base_url(); ?>admin/edit_driver">
<div class="modal-body" id='add'>
<div class="row" id="form_pesan">
<input type="hidden" name="id" class="form-control id" />
<div class="col-sm-6">
<input type="email" class="form-control input-lg email" name="email">
</div>
<div class="col-sm-6">
<input type="text" class="form-control input-lg name" name="name" >
</div>
<div class="col-sm-6">
<input type="text" class="form-control input-lg phone" name="phone">
</div>
<div class="col-sm-6">
<select class="form-control" id="location_name" name="location_name">
<?php foreach ($vendor as $v) {
?>
<option id="<?php echo $v['location_id']; ?>" value="<?php echo $v['location_id']?>" ><?php echo $v['name'] ;?></option>
<?php }; ?>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="add" type="submit" class="btn btn-primary btn-md">Save</button>
</div>
我的jquery:
$(document).on( "click", '.edit_button',function(e) {
var email = $(this).data('email');
var id = $(this).data('id');
var name = $(this).data('name');
var phone = $(this).data('phone');
var location = $(this).data('location');
var locationid = $(this).data('locationid')
$(".id").val(id);
$(".email").val(email);
$(".name").val(name);
$(".phone").val(phone);
$(".location").val(location);
$(".locationid option:selected").val(locationid);
});
控制器:
public function edit_driver()
{
if(!$this->user_permission->check_permission())return;
$user_email = $this->input->post('email');
$id = $this->input->post('id');
$phone = $this->input->post('phone');
$location_id = $this->input->post('location_id');
$data_user = array(
'user_email' => $user_email,
'user_id' => $id,
'user_phone' => $phone
);
$this->db->where('user_id',$id);
$this->db->update('user', $data_user);
if($this->db->affected_rows() > 0){
$data_driver = array(
'user_id' => $id,
'location_id' => $location_id
);
$this->db->where('user_id',$id);
$this->db->update('user_driver',$data_driver);
redirect('admin/user/user_driver');
}
}
它会抛出database error: location_id cannot be null
,因为我的代码不会将location_id
发送给控制器,但其他值如email
name
和phone
工作良好。我不知道如何解决这个问题。
另外,我添加了存储select
查询的控制器:
$this->db->select("d.user_id as id, d.plate_number as plate_number, d.current_lat as lat, d.current_lon as lon, d.created_on as created_on, d.updated_on as updated_on, d.available as available, d.location_id as location_id, u.user_name as name, u.user_email as email, u.user_phone as phone, v.name as location_name");
$this->db->from('user_driver as d');
$this->db->join('user as u', 'd.user_id = u.user_id','left');
$this->db->join('vendor_location as v', 'd.location_id = v.location_id','left');
$query = $this->db->get();
$data['driver'] = $query->result_array();
$this->db->select("location_id, name");
$this->db->from('vendor_location');
$query2 = $this->db->get();
$data['vendor'] = $query2->result_array();
答案 0 :(得分:0)
[求助]我将$(this).find(':selected').attr('data-id')
添加到我的jquery,并将id
中的name
和<select>
属性更改为locationid
以及