0

I created this php for administrators to search for users and allow administrators to edit their details when they click the button. When the user is not using the search function, the "Update" button will work. After the user types the search function, it will display all relevant fields according to the user input, but after the user uses the search function, the "Update" button will no longer work. Thank you.

<?php
include '_base.php';
$p->title = 'List of Users';


$nameorid = $p->get('nameorid');

$stm = $db->prepare('SELECT * FROM user WHERE id LIKE ? OR `name` LIKE ? OR email LIKE ?');
$stm->execute(["%$nameorid%","%$nameorid%","%$nameorid%"]);
$arr = $stm->fetchAll();

// output -----------------------------------------------------------------------------------------
include '_head.php';
?>

<h1 margin-left:0px;>List Of Users</h1>

<?= $h->label('User ID / Name / Email ') ?>
<?= $h->text('nameorid', null, 50, 'autofocus') ?>


<div id="target">


<p><?= count($arr) ?> record(s)</p>

<table class="utable">
<thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Group</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php
            foreach ($arr as $s) {
                echo "
                    <tr>
                        <td>$s->id</td>
                        <td>$s->name</td>
                        <td>$s->email</td>
                        <td>$s->role</td>
                        <td>
                        <button id='normalBtn' data-get='/updateUser.php?id=$s->id&role=$s->role'>Update</button>
                        </td>
                    </tr>
                ";
            }
        ?>
    </tbody>
</table>
</div>

<script>

    let nameorid;

    $('#nameorid').on('input', function (e) {
        nameorid = $(this).val().trim();

        let param = $.param({
            nameorid: nameorid
        });

        let url = `/userList.php?${param} #target`;

        $('#target').load(url, highlight);
    });

    function highlight(){
        if (nameorid == '') return;


        let re = new RegExp(escapeRegExp(nameorid), 'gi');


        $('td:nth-child(2)').each(function () {

            let h = $(this).html().replace(re, '<mark>$&</mark>');
            $(this).html(h);
        });

        $('td:nth-child(1)').each(function () {

            let h = $(this).html().replace(re, '<mark>$&</mark>');
            $(this).html(h);
        });

        $('td:nth-child(3)').each(function () {

            let h = $(this).html().replace(re, '<mark>$&</mark>');
            $(this).html(h);
        });
    }
</script>


<?php
include '_footer.php';
  • You need to use a delegate event binding. `$('#target').load(url, highlight);` is going to replace all the elements that you created direct bindings on. – Taplar Apr 21 '20 at 18:00
  • Element IDs should be unique. ` – aynber Apr 21 '20 at 18:01

0 Answers0