-1

I need to select a record in the modal form. The HTML code is,

<table class="table justify-content-center" style="background-color:white">
                    <input type="hidden" name="id" value="<?php echo $id; ?>">
                    <thead style="background-color:green;color:white;font-weight:bold">
                        <tr>
                            <th>Employee Name</th>
                            <th>Employee ID</th>
                            <th>Designation</th>
                            <th>Phone</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
            <?php
                while ($row = $result->fetch_assoc()): ?>
                    <tr>
                        <td><?php echo $row['employeename']; ?></td>
                        <td><?php echo $row['employeeno']; ?></td>
                        <td><?php echo $row['designation']; ?></td>
                        <td><?php echo $row['contactnumber']; ?></td>
                        <td>
                            
                            <button name="edit" class="editbtn"> <a href="#editEmployeeModal"  data-toggle="modal" data-dismiss="modal" style="color: brown"  >Edit</a> </button>
                        
                        <button name="delete" class="btn btn-info " ><a href="employeelist.php?delete=<?php echo $row['id']; ?>" style="color: brown">Delete</a> </button> 
                        </td>
                    </tr>
                <?php endwhile; ?>    
                </table>

and the HTML for modal form is,

<!-- Edit Modal HTML -->
    <div id="editEmployeeModal" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="employeeprocess.php" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="id" value="<?php echo $id; ?>">
                    <div class="modal-header">                      
                        <h4 class="modal-title">Edit Employee</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    <div class="modal-body">                    
                        <div class="form-group">
                            <label>Employee Number</label>
                            <input type="text" class="form-control" value="<?php $employeenum ?>" name="employeenum" required>
                        </div>
                        <div class="form-group">
                            <label>Employee Name</label>
                            <input type="text" class="form-control" name="employeename" value="<?php $employeename ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Job Location</label>
                            <input type="text" class="form-control" name="joblocation" value="<?php $joblocation ?>" required>
                        </div>
                        <div class="form-group">
                            <label>DateofJoin</label>
                            <input type="date" class="form-control" name="dateofjoin" value="<?php $dateofjoin ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Contact Number</label>
                            <input type="text" class="form-control" name="contactnumber" value="<?php $contactnumber ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Designation</label>
                            <input type="text" class="form-control" name="designation" value="<?php $designation ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Status</label>
                            <input type="text" class="form-control" name="status" value="<?php $status ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Photo</label>
                            <input type="file" class="form-control" name="myfile" required>
                        </div>                  
                    </div>
                    <div class="modal-footer">
                        <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel">
                        <input type="submit" class="btn btn-success" name="edit" value="Update">
                    </div>
                </form>
            </div>
        </div>
    </div>

employeeprocess.php

if (isset($_GET['delete'])){
    $id = $_GET['delete'];
    $mysqli->query("DELETE FROM employees WHERE id=$id") or die($mysqli->error());
    
    $_SESSION['message'] = "Record has been deleted!";
    $_SESSION['msg_type'] = "danger";
    
    header("location: employeelist.php");
}
if (isset($_GET['edit'])){
    $id = $_GET['edit'];
    $update = true;
    $result = $mysqli->query("SELECT * FROM employees WHERE id=$id") or die($mysqli->error());
    if (count($result)==1){
        $row = $result->fetch_array();
        
        $employeenum = $row['employeenum'];
        $employeename = $row['employeename'];
        $joblocation = $row['joblocation'];
        $dateofjoin = $row['dateofjoin'];
        $contactnumber = $row['contactnumber'];
        $designation = $row['designation'];
        $status = $row['status'];
    }
}

Javascript is

<script>    

    $(".editbtn").click(function () {
        $('#edit').val($(this).data('id')); 
    
    });    
    </script>

I need to pass the selected record to the editEmployeeModal and set the variable edit for modal dialog to display the selected record. The modal is not working, if I put the statement href="#editEmployeeModal?edit= <?php echo $row['id']; ?>" So, Can you suggest me to this problem?

  • you need to use javascript, you added jquery to the tags but you are not using it. – Alex Angelico Jun 28 '20 at 03:33
  • I have tried javascript, but the script is not working correctly. Can you provide the correct javascript? – Balamurugan Jun 28 '20 at 03:37
  • I have added the javascript which I tried. I just need the edit variable set to the selected id of the row to be displayed in modal form – Balamurugan Jun 28 '20 at 04:57
  • You have an error. `$mysqli->error()` is not a function. It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Jun 28 '20 at 12:47
  • I haven't used any $mysqli->error() in my code. See the code properly before giving comment – Balamurugan Jun 28 '20 at 13:20

1 Answers1

1

You can set a data attribute as id of your row in the for loop and bind an onClick event to a js function to update the form id field:

   <button name="edit" class="btn btn-info"> <a href="#editEmployeeModal"  data-toggle="modal" data-dismiss="modal" style="color: brown" data-rowid="<?=$row['id']; ?>" onClick="updateId()" >Edit</a> </button>

JavaScript

function updateId(){
    let id = this.getAttribute('data-rowid');
    document.getElementsByName("id")[0].value = id;
}

You could alternatively pass the id to the updateId function as a parameter instead of setting the data attribute.

Rishabh Jha
  • 24
  • 12