Passing php variable to modal twitter bootstrap not working

前端 未结 1 1872
情歌与酒
情歌与酒 2020-12-22 03:48

Hi I\'m passing php variable to twitter boostrap modal. but I cant get the right id when I click the link inside the modal. please see code below. first I have created a fo

相关标签:
1条回答
  • 2020-12-22 04:29

    I have made quite some edits to your code, let's start with what you were doing first:

    1. <td>".$nattco['address']."</td> </td>";

      This has to be </tr> instead of </td>.

    2. You only need one modal box. We will pass the id for them through jQuery. So create only 1 static modal box at the end of your table.

    Now the things you need to do:

    1) Make use of this $id = $nattco['id']; as your <tr id> so it will be:

     $id = $nattco['id'];
     echo "<tr id='".urlencode(htmlspecialchars($id))."'> #...rest of your code
    

    2) Give a class for your buttons in the modal box:

    <a href='update_sub_agent.php' class='btn btn-success edit'><i class='icon-pencil'></i> Edit</a>
                                                     <!-- ^edit class added -->
    <a href='owner.php' class='btn btn-info owner'><i class='icon-user'></i> Owner</a>
                                       <!-- ^owner class added -->
    <a href='fla.php' class='btn btn-warning fla'><i class='icon-list-alt'></i> FLA</a> 
                                        <!-- ^fla class added -->
    <a href='delete.php' class='btn btn-danger delete'><i class='icon-remove'></i> Delete</a>
                                          <!-- ^delete class added -->
    

    3) Add href and class to your` tag :

    `<td><a href='#myModal' class='b modal-box'>".$nattco['agent_name']."</a></td>`
    

    And finally, the jQuery bit:

    $('a.modal-box').click(function(e){
       id=$(this).closest('tr').attr('id');
       $('.modal-body .edit').attr('href','update_sub_agent.php?id='+id+'');
       $('.modal-body .owner').attr('href','owner.php.php?id='+id+'');
       $('.modal-body .fla').attr('href','fla.php?id='+id+'');
       $('.modal-body .delete').attr('href','delete.php?id='+id+'');
    });
    

    That's all. This minimizes your code so that modal box is not repeated while dynamically passing id of the clicked row.

    You can have a look a the demo here:

    DEMO

    0 讨论(0)
提交回复
热议问题