how can i refresh just the modal with ajax call without losing JavaScript tag

Viewed 32

so im working on this library project where students can order books and admin has to confirm those requests from his side. here is a pic of the project to have a small idea: those are students requests

when u click on the blue button you can see more details of the student order:order details

as you can see there is the book that the student has ordered. now what i wanted is to refresh just the modal after the admin click accept and i did achieve that by using the .load() function here is the piece of code that responsible that :code that load the modal
since we have multiple students and they can order multiple books so i had to use 2 loops that why you see those dynamic id on the modal id and button id, $tmp is for the students loop and $tmp2 is for the books loop. so far so good, the problem is when the modal refresh i lose my script tag which is bad why because all my buttons have click events so no script means buttons will not work here is before and after modal refresh before refresh you can see the script tag that responsible for the buttons and the ajax call here is after as you can see the modal refresh successfully but i lost my script so the buttons will not work so i have to reload the whole page so i can retrieve my script then the buttons will work again.
some of the solutions i tried is to put the script outside of the modal because i assumed .load() can only fetch html no script but i can't because i need the script to be inside that loop in the modal for the books button

here is the modal code if you want more details :

                                <!--  user books details modal -->
                       
                                <div class="modal fade" id="userBooksDetails<?= $tmp ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                    <div class="modal-dialog modal-lg ">
                                        <div id="modalContent<?= $tmp ?>" class="modal-content">
                                            <div class="modal-header">
                                                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                                                <button type="button" class="btn-close" data-mdb-dismiss="modal" aria-label="Close"></button>
                                            </div>
                                            <div id="modalBody<?= $tmp ?>" class="modal-body">

                                                <table class="table">
                                                    <thead>
                                                        <tr>
                                                            <th scope="col">Picture</th>
                                                            <th scope="col">Name</th>
                                                            <th scope="col">Status</th>
                                                            <th scope="col">Action</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <?php
                                                      //dont mind this its just to fetch information of the book to display it in the modal
                                                        $livre = new  Livre();
                                                        $exemplaire = $livre->myBooks($user->id);
                                                    
                                                        if ($exemplaire) {
                                                            //here is the second loop because student can order multiple books and each book request need to have an unique id for it button 
                                                            foreach ($exemplaire as $exem) {

                                                                $tmp2--;
                                                                $copy = Exemplaire::find($exem['Num_Exemp']);
                                                                $book = Livre::find($copy->ISBN);
                                                        ?>
                                                        <script>
                                                                    $(document).ready(function() {
                                                                       
                                                                      
                                                                        
                                                                        //request for accepting the order of the user  
                                                                        $("#acceptBook<?= $tmp2 ?>").click(function() {
                                                                            let copyId = $(this).attr("data-idCopy");

                                                                            $.ajax({
                                                                                url: "<?= PROOT ?>emprunts/acceptOrder/" + copyId,
                                                                                type: "POST",
                                                                                success: function(data) {
                                                                                    $("#modalContent<?= $tmp ?>").load(window.location.href + " #modalContent<?= $tmp ?>" );
                                                                                    console.log(data)
                                                                                    
                                                                                }
                                                                            });
                                                                        });
                                                                        

                                                                        //request for rejecting the order of the user 
                                                                        $("#rejectBook<?= $tmp2 ?>").click(function() {
                                                                            let copyId = $(this).attr("data-idCopy");

                                                                            $.ajax({
                                                                                url: "<?= PROOT ?>emprunts/rejectOrder/" + copyId,
                                                                                type: "POST",
                                                                                success: function(data) {
                                                                                    //$("#userBooksDetails<?= $tmp ?>").load(" #userBooksDetails<?= $tmp ?>");
                                                                                    console.log(data);
                                                                                    $("#modalBody<?= $tmp ?>").load(window.location.href + " #modalBody<?= $tmp ?>" );

                                                                                }
                                                                            });
                                                                        });

                                                                        //request for returning the book (is not working after reloading on accept fix it )
                                                                        $("#return<?= $tmp2 ?>").click(function() {
                                                                            let copyId = $(this).attr("data-idCopy");

                                                                            $.ajax({
                                                                                url: "<?= PROOT ?>emprunts/returnBook/" + copyId,
                                                                                type: "POST",
                                                                                success: function(data) {
                                                                                    console.log(data);
                                                                                    $("#modalBody<?= $tmp ?>").load(window.location.href + " #modalBody<?= $tmp ?>" );

                                                                                }
                                                                            });
                                                                        })
                                                                    })
                                                                </script>

                                                                <tr>
                                                                    <th> <img src="<?= PROOT . "/public/img/books/" . $book->img ?>" class="w-50 rounded-t-5 rounded-tr-md-0 rounded-bl-md-5" aria-controls="#picker-editor">
                                                                    </th>
                                                                    <td class="display-6 text-align-center">
                                                                        <div class="my-5">
                                                                            <?= $book->Titre_livre ?>
                                                                        </div>
                                                                    </td>
                                                                    <td>
                                                                        <div class="form-group  mt-4">
                                                                            <button id="stat<?= $tmp2 ?>" disabled class="btn mt-5"><?php if($exem['isConfirm']==0)
                                                                                            echo "Not Confirmed";
                                                                                            else echo "Confirmed"; ?></button>      
                                                                        </div>

                                                                    </td>
                                                                    <td id="test<?= $tmp2 ?>">
                                                                  <!--  here im checking if the book is confirmed if yes im showing some specific buttons if not hiding some buttons -->
                                                                        <div class="form-group  mt-5">
                                                                            <a <?php if($exem['isConfirm']==1)
                                                                                            echo "hidden";
                                                                                            else
                                                                                            echo "" ?> 
                                                                                            id="acceptBook<?= $tmp2 ?>" class="btn  m-2" data-idCopy="<?= $exem['Num_Exemp'] ?>"  role="button">Accept</a>
                                                                            <a <?php if($exem['isConfirm']==1)
                                                                                            echo "hidden";
                                                                                            else
                                                                                            echo "" ?>  id="rejectBook<?= $tmp2 ?>" class="btn btn-dark m-2" data-idCopy="<?= $exem['Num_Exemp'] ?>" role="button">Reject</a>
                                                                            <a <?php if($exem['isConfirm']==0)
                                                                                            echo "hidden";
                                                                                            else
                                                                                            echo "" ?>  id="return<?= $tmp2?>" data-idCopy="<?= $exem['Num_Exemp'] ?>"  class="btn btn-secondary mt-4"> Return </a>

                                                                        </div>
                                                                    </td>
                                                                </tr>
                                                        <?php }
                                                        }
                                                        ?>
                                                    </tbody>
                                                </table>



                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary" data-mdb-dismiss="modal">Close</button>

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






0 Answers
Related