2016-09-27 16 views
1

私は3 input type="textと1 select optionを含む編集フォームを持っています。 input textの値は、データベーステーブルに更新しようとすると問題なく動作します。しかし、私はselectオプションで問題があります。データ属性の入力選択オプションを

私は、HTML buttondata-*属性を使用しています:

<?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'); 
    } 
} 

私のコードは何とかコントローラにlocation_idを送信しないので、それはdatabase error: location_id cannot be nullをスローしますが、emailnamephoneのような他の値が正常に動作します。私はこれを解決する方法を知らない。追加については

、私は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(); 
+1

同様locationid<select>idname属性を変更[SOLVED]あなた(location_id '=> $ location_id); 'を更新する前にそのキーペアを追加するだけです。 – Ghost

+0

' update(' user ') 'は' location_id'を更新しませんでした'location_id'という名前の列がないためです。 'location_id'は' update( 'user_driver') 'テーブルで更新されるべきです。私の質問があなたのために十分明確でない場合はごめんなさい@Ghost – may

+0

'user_driver'アップデートの前に' location_id'を追加するだけです。 '$ data ['location_id'] = $ location_id'、その宣言を追加します。 'user'に含まれていない場合は' user'を更新してください。 – Ghost

答えて

0

私はjqueryのに$(this).find(':selected').attr('data-id')を追加し、

関連する問題