I am retrieving data from MySQL table and displaying in a HTML Table by <?php foreach ?> loop
like below.
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
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);
) ?
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.
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.