Pages

Showing posts with label View. Show all posts
Showing posts with label View. Show all posts

11 Apr 2016

Auto Save PHP Form Using Jquery Autoave Funtion - Codiginter Framework

Jquery Script

<script type="text/javascript">
// JQUERY: Plugin "autoSumbit"
(function($) {
$.fn.autoSubmit = function(options) {
return $.each(this, function() {
// VARIABLES: Input-specific
var input = $(this);
var column = input.attr('name');
$('.alert-success').hide();
// VARIABLES: Form-specific
var form = input.parents('form');
var method = $('#form_profile_1').attr('method');//form.attr('method');
var action = $('#form_profile_1').attr('action'); //form.attr('action');

// VARIABLES: Where to update in database
var where_val =$('#where').val();
var where_col = $('#where').attr('name');

// ONBLUR: Dynamic value send through Ajax
input.bind('change', function(event) {
// Get latest value
var value = input.val();
// AJAX: Send values
$.ajax({
url: action,
type: method,
data: {
val: value,
col: column,
w_col: where_col,
w_val: where_val
},
cache: false,
timeout: 10000,
success: function(data) {
// Alert if update failed
if (data) {
alert(data);
}
// Load output into a P
else {
//alert('hear');
$('.alert-success').show();
//$('.alert-success').text('Updated');
//$('.alert-success').fadeOut();
$(".alert-success").fadeIn('slow').delay(5000).fadeOut('slow');
}
}
});
// Prevent normal submission of form
return false;
})
});
}
})(jQuery);
// JQUERY: Run .autoSubmit() on all INPUT fields within form
$(function(){
$('#form_profile INPUT').autoSubmit();
});
</script>



Controller



 public function update()
{
if (count($_POST))
{
 
$col= $this->input->post('col');
$val= $this->input->post('val');
$w_col= $this->input->post('w_col');
$w_val= $this->input->post('w_val');
$update_data= array(
$col => $val
);
// $update_query="update tbl_employee set ".$col."='".$val."' where ".$w_col."='".$w_val."'";
// echo $update_query;
// $this->db->update($update_query);
$this->db->where($w_col,$w_val);
$this->db->update('tbl_employee',$update_data);
}

}


View


<div >
 
<input id="where" type="hidden" value="<?=$r->id?>" name="id">
<input type="text" class="form-control input-sm" value="<?=$r->emp_firstname?>" id="emp_firstname" name="emp_firstname" style="width:60%" placeholder="First Name" autocomplete="off">
                                                    
                                                         
<input type='text' name="emp_middle_name" value="<?=$r->emp_middle_name?>" id="emp_middle_name" class="form-control input-sm" style="width:60%" placeholder="Middle Name" />
 
</div>