Update old records from database using jQuery in you codeigniter web application is very simple. Insert new value in the form and replace data using update query using jQuery Ajax.
Create a PHP web application you can use codeigniter framework in your LocalServer or web hosting, you can insert, select, delete or update using jQuery in programming language.
In this tutorial I will help you how to update MySQL table records using jQuery with source code and with an example. Follow the example.
First you need to put old value in a form and change value which you want to change for replace or edit. That’s like:
Update Form:
<div class="row"> <h2>Update record into MySQL table using jQuery</h2> <div class="form-group"> <label class="control-label"> Name </label> <input type="hidden" class="form-control" id="id" value="<?php echo $pro->id; ?>"> <input type="text" class="form-control" id="name" value="<?php echo $pro->name; ?>"> </div> <button class="" type="button" id="update_data"> Update Submit </button> </div>
Script:
Access controller page and define a function and pass hidden id value and form value in the controller page. That’s like
<script type="text/javascript"> $('#update_data').click(function(){ $.ajax({ type: 'post', url: '<?php echo base_url("Controller/update_data");?>', data: {id:$('#id').val(), name:$('#name').val()}, success: function(result){ if(result=='1'){ alert("Update Successful"); }else{ alert("Update Error!!"); } } }); }); </script>
Controller:
Access model and define a function and go to the model page.
<?php public function update_data(){ $this->Model->updateData(); } ?>
Model:
Store input value an array variable and update records into database and if data update successful then return a success message or return error message.
<?php public function updateData(){ $data= array("name" => $this->input->post('name')); $this->db->where('id',$this->input->post('id'))->update('Table name',$data); if($qry){ echo '1'; }else{ echo '0'; } } } ?>
Great Post