Kushal Srivastava February 2016

How to pass current row value in modal?

I am perfoming PHP CRUD operations on a table. When i click the edit button rather then taking it to a new page i want to show the values in a modal. I want the value's to be displayed in the modal's form. I have created a modal but i am unable to think of an logic to pass the values of the row in which the edit button was clicked. Any help would be highly appreciated.

Table:

<table class="table datatable-basic table-bordered table-hover">
    <thead>
        <tr class='active'>
            <th><b>S.No.</b></th>
            <th><b>Name</b></th>
            <th><b>Mobile Number</b></th>
            <th><b>Password</b></th>
            <th><b>Actions</b></th>
        </tr>
    </thead>

    <tbody>
        <?php 

            $sql="SELECT * from users ORDER BY name ASC" ; 
            $c = 1;
            $results = $result->query($sql);
            while($row = $results->fetch_assoc()) 
            { 
             echo '<tr style="font-weight:normal;">'; 
             echo "<td>$c</td>"; 
             echo "<td>{$row['name']}</td>"; 
             echo "<td>{$row['username']}</td>"; 
             echo "<td>{$row['pass']}</td>"; 

             echo "<td class='text-center'><ul class='icons-list'><a href='#' style='color:#000;'><i class='icon-pencil5' data-toggle='modal' data-target='#modal_edit'
             data-popup='tooltip' title='Edit' data-container='body'></i></a>

             <a href='delete.php?teacherid={$row['username']}' style='color:#000;'><i class='icon-cross2' data-popup='tooltip' title='Delete' 
             data-container='body'></i></a></ul></td>"; 
             echo '</tr>';
               ++$c; 
            } 
           

Answers


Nana Partykar February 2016

<?php 
    .
    .
    echo "<td class='text-center'>
            <ul class='icons-list'>
                <a href='#modal_edit' class='modalButton' style='color:#000;' data-teacherid='{$row['username']}' data-toggle='modal' data-target='#modal_edit' data-popup='tooltip' title='Edit' data-container='body'>
                    <i class='icon-pencil5'></i>
                </a>";

    echo "<a href='delete.php?teacherid={$row['username']}' style='color:#000;'><i class='icon-cross2' data-popup='tooltip' title='Delete' 
    data-container='body'></i></a></ul></td>"; 
    echo '</tr>';
    ++$c; 
} 
?>

Place this code somewhere, but in the same page.

<div id="modal_edit" class="modal fade" style="font-weight: normal;">
    <div class="modal-dialog">
          <div class="modal-content">

          </div>
    </div>
</div>

JS

<script>
    $('.modalButton').click(function(){
        var teacherid = $(this).attr('data-teacherid');
        $.ajax({url:"ajax_modal_edit.php?teacherid="+teacherid,cache:false,success:function(result){
            $(".modal-content").html(result);
        }});
    });
</script>

Create a page name ajax_modal_edit.php (If you are looking to change this page name. Change in <script></script> tag too. Both are related.)

ajax_modal_edit.php

<?php
$teacherid = $_GET['teacherid'];

// Use this `$teacherid` in query to get all required/appropriate field
?>

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h5 class="modal-title">Add Teacher <?php echo $teacherid;?></h5>
</div>
<form action="" method="POST">
    <d 

Post Status

Asked in February 2016
Viewed 2,738 times
Voted 11
Answered 1 times

Search




Leave an answer