php - Passing one of the <td> value to controller when clicking a button -


i have table this:

enter image description here

when click delete! button, want pass role id controller.

after looking lot of cases in stackoverflow. i've succeeded retrieve role id using js.

code this:

$(".deletebutton").click(function() {   var $row = $(this).closest("tr");    var $text = $row.find("td:first-child").text();    // let's test out   alert($text);  }); 

but problem is, don't how pass button, button can pass controller.

is there simple way pass controller? maybe without retrieving role id first, , when click button value passed , controller called @ once?

this code:

<table class="table" id="mytable">    <thead class="text-primary">        <th class="col-md-1">id role</th>        <th class="col-md-3">role</th>        <th class="col-md-3">jumlah member</th>        <th></th>    </thead>     <tbody>    <?php        foreach($result $row) { ?>        <tr>            <td><?php echo $row->id_role ?></td>            <td><?php echo $row->role ?></td>            <td><?php echo $row->jumlah ?></td>            <td>                <button type="button" class="btn btn-success btn-sm editbutton">edit!</button>                <button  type="button" class="btn btn-danger btn-sm deletebutton">delete!</button>             </td>        </tr>        <?php          }  ?>     </tbody> </table> 

do have use javascript? if not, simple task form

<form action="path/to/your/controller" method="post"> <table class="table" id="mytable"> <!-- thead, etc --> <tbody> foreach($result $row) : ?> <tr>   <td><?= $row->id_role ?></td>   <td><?= $row->role ?></td>   <td><?= $row->jumlah ?></td>   <td>     <button type="submit" name="edit" value="<?= htmlspecialchars($role->id_role) ?>"             class="btn btn-success btn-sm editbutton">edit!</button>     <button type="submit" name="delete" value="<?= htmlspecialchars($role->id_role) ?>"             class="btn btn-danger btn-sm deletebutton">delete!</button>   </td> </tr> <?php endforeach ?> </tbody> </table> </form> 

your controller receive either $_post['edit'] or $_post['delete'] role id value.

if you're interested in securing form possible cross-site-request-forgery attacks, codeigniter has built-in solution.


otherwise, add id_role property button, eg

<button type="button" class="btn btn-danger btn-sm deletebutton" value="<?= htmlspecialchars($role->id_role) ?>">delete!</button> 

and in js

$('.deletebutton').on('click', function(e) {   var roleid = this.value   $.ajax({     url: '/roles/' + roleid, // assuming rest api     method: 'delete'   }).done(function() {     alert('deleted role ' + roleid);   }) }) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -