0

I am retrieving data from MySQL table and displaying in a HTML Table by <?php foreach ?> loop like below.

Displaying data in HTML Table from MySQL in Codeigniter

Now I want to edit/delete the data of a particular row of the above HTML table by pressing the edit or delete button of the table. My MySQL table primary key is branch_code.

I know how to update/delete data in MySQL table in Codeigniter but I do not know how to retrieve the data from the table row. I could not try because I do not know how to start it.

I found similar questions like

  1. Get values from html table using codeigniter
  2. PHP-CodeIgniter: How to get the corresponding html table row values to be deleted by Javascript

In No#1 it was written Collect all this data in an array and "send" it to your PHP via an Ajax POST request. ----- ok great, but how could I do that ?

Can you please help me ?

May be my question is duplicate, but I didn't get any answer from those originals or I would say, I am unable to proceed the process.

Thank you in advance.

Update:

from the No#2 I got the below...

$(document).on('click', '#deleteRow', function() {
  var obj = $(this).parent().parent();
  var companyId = obj.attr('branch_code');
  obj.remove();
});

I understood that I should add unique attribute to <tr>.

Now how can I pass the value in a variable to a model so that I can update my MySQL table ($this->db->where('branch_code', $branch_code);) ?

Community
  • 1
  • 1
Raja
  • 772
  • 1
  • 15
  • 38

1 Answers1

0

The way I usually do this is when you make render the html table you would create a unique id for each <tr> , so you'd do something like this:

<?php foreach ( $table->result() as $row): ?>
    <tr id="branch_<?= $row->branch_code ?>">
        <td>Whatever you want here</td>
        <td><a href="javascript:deleteBranch(<?= $row->branch_code; ?>)"></a></td>
    </tr>
<?php endforeach; ?>

Then you can do an AJAX call to delete and grab that id with it.

<script>
function deleteBranch(id) {
    $.post('controller_url', {"branch_code" : id }, function(data){
       //Your controller method would echo a 1 for success
       if (data == 1) {
           $("#branch_" + id).fadeOut();
           //show some kind of message
       }
    });
}
</script>

So from the controller you could now have a $this->input->post("branch_code") that you could reference to get the branch to delete.

acupofjose
  • 2,159
  • 1
  • 22
  • 40
  • Thank you Schultzi for your response. Let me first try your method. Another thing is, as php is a server side code, it cannot be viewed in browser view source. But javascript can be viewed. So is it possible to hide this code from page source view ? – Raja Feb 11 '15 at 03:46
  • No, there's not really a way to hide it. You can obscure it, or uglify it (http://stackoverflow.com/a/194399/3629438), but anything done client side will be able to be seen by the client, it's only how much the client is willing to do to see your code. If my answer helped, please mark it as the accepted! I'm more than happy to help in any way that I can – acupofjose Feb 11 '15 at 04:35