0

i have a website in codeigniter php, i have a form inside it where user can add and edit products, everything is working fine, my edit product code is like below:

View:

<form action="" method="post" enctype="multipart/form-data">
  <div class="row">

    <div class="form-group col-md-12">
      <p style="font-size: larger;">Mandatory Fields</p>
    </div>
    <div class="form-group col-md-6">
      <label>Product Name*</label>
      <input type="text" name="productname" class="form-control" value="<?= $val->productname?>" id="inputEmail4" required>
    </div>
    <div class="form-group col-md-6">
      <label>Product Code*</label>
      <input type="text" name="productcode" class="form-control" value="<?= $val->productcode?>" id="inputEmail4" required>
    </div>
    </div>
<button type="submit" id="thatbutton" name="editproduct" class="btn btn-primary">SAVE</button>
</form>

controller:

if(isset($_POST['editproduct']))
{




$productname = $this->input->post('productname');
$productcode = $this->input->post('productcode');


$insertUserDatas = $this->category->editproduct($productname,$productcode,$id);

//Storing insertion status message.
if($insertUserDatas){
$this->session->set_flashdata('success_msg', 'Product Edited Successfuly');
}else{
$this->session->set_flashdata('error_msg', 'Some problems occured, please try again.');
}


redirect('products');

}

Model:

public function editproduct($productname,$productcode,$id) {
$this->db->where("id", $id);
$this->db->update('product', array('productname' => $productname, 'productcode' => $productcode));
return true;
}

what i want here is, when a user edits the product, get that particular edited field and save in database, like 'The user edited the product code', is it possible, thanks in advance

TBC
  • 101
  • 7
  • Why you are making your life difficult, keep it simple fetch all posted information and save or update. if anything changed will be replaced in your database if nothing changed it will remain same. – umefarooq Jul 10 '23 at 11:36
  • 1
    Does this answer your question? [jQuery Ajax POST example with PHP](https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Mepix Jul 11 '23 at 04:57

1 Answers1

1

You can try this. Add data-oldval attribute to your input fields. In the JS compare this with the current value. Add one hidden changedfields input field. Then compare it to the current input value, if some changes are there push it to an array and set it into the hidden changedFields field. The hidden field will be submitted by form post

$('input[type=text]').change(function() {
    var changedFields = $("#changedFields").val();
    changedFields = changedFields == '' ? [] : changedFields.split(",");
    
    oldVal = $(this).attr('data-oldval');
    curVal = $(this).val();
    
    if(oldVal != curVal) {
        if(!changedFields.includes($(this).attr('name'))) {
            changedFields.push($(this).attr('name'));
        }
        $(this).css("background",'#fff3b5');
    } else {
        var changedFields = changedFields.filter(e => e !== $(this).attr('name'))
        $(this).css("background",'#fff');
    }
    
    $("#changedFields").val(changedFields.toString());
    
    console.log(changedFields);
}) 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="post" enctype="multipart/form-data">
  <div class="row">
    <div class="form-group col-md-12">
      <p style="font-size: larger;">Mandatory Fields</p>
    </div>
    <div class="form-group col-md-6">
      <label>Product Name*</label>
      <input type="text" name="productname" class="form-control" value="<?= $val->productname?>" data-oldval="<?= $val->productname?>" id="productname" required>
    </div>
    <div class="form-group col-md-6">
      <label>Product Code*</label>
      <input type="text" name="productcode" class="form-control" value="<?= $val->productcode?>" data-oldval="<?= $val->productcode?>" id="productcode" required>
    </div>
    </div>
    <input type="hidden" name="changedFields" id="changedFields" value="">  
  <button type="submit" id="thatbutton" name="editproduct" class="btn btn-primary">SAVE</button>
</form>

`

shirshak007
  • 433
  • 1
  • 8