简体   繁体   中英

codeigniter update database record using ajax

I need update database record using form via ajax,I have tried several ways. But I'couldnt do it using post method. here is my code

view

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <h2>Company Details.</h2>        

    <?php
          $this->load->helper('form');
          $attributes = array('method'=>'post','name'=>'update_company','id'=>'update_company');
          echo form_open_multipart('',$attributes);?>
    <?php// echo form_open_multipart('site/update');?>
    <?php $sid=$company_data->id;?>
    <label>cmp_id : </label> <?php echo form_input('sid',$sid);?><br/><br/>
    <label>Code : </label> <?php echo form_input('code',$company_data->code);?><br/>   <br/>
    <label>Name : </label> <?php echo form_input('name',$company_data->name);?><br/>  <br/>
    <label>Logo : </label><input type="file" name="userfile"/><br/><br/>
    <label>URL : </label> <?php echo form_input('url',$company_data->url);?><br/><br/>
    <label>Description : </label> <textarea name="description" rows="4" cols="50"><?php     echo $company_data->description; ?></textarea><br/><br/>
    <input type="submit" value="Update"/>
    </form>

    <script>   //no need to specify the language
       $(document).ready(function() {

       $('#update_company').on("submit",function(e) {

            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "<?php echo site_url('site/update/'); ?>",
                data: $(this).serialize(),
                success: function(data){
                    var site_url = "<?php echo site_url('site/update/'); ?>";
                    var id = "<?php echo $sid; ?>";
                    site_url = site_url +"/" + id;
                    alert(site_url);
                    alert(id);
                        //$("#content").load("http://localhost/FinanceManagementSystem/index.php/site/update/"+id);
                }
           });            
        });
      });
    </script>

according to the $sid I need to update that particular record, contorller

public function update(){

        $this->load->model('company_model');
        $updateName=$this->company_model->update_company();

         return $updateName;   
    }

model here need to access $id using post method which is sent by from in view.

  function update_company(){
        //user details
        $username =  $this->session->userdata('username');
        $query =  $this->db->get_where('userdetails',array('username'=>$username));


    $company_update_data = array(
        'code' => $this->input->post('code'),
                    'name' => $this->input->post('name'),
                    //'logo' => $imgpath['file_name'],
                    'url' => $this->input->post('url'),
                    'description' => $this->input->post('description'),
                    'createdat' => date('Y-m-d H:i:s',strtotime('')),
                    'status' => 0,
                    'userid' => $this->userid
             );
        $id = $this->input->post('id');
        $this->db->where('id', $id);
        $update = $this->db->update('companydetails', $company_update_data); 
        return $id;
}

I think your problem is that you have no index 'id'

$id = $this->input->post('id');

try dumping your post and see if you have provided $_POST["id"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM