首页 > 解决方案 > 将带有键的 PHP 变量传递给 Bootstrap 模式

问题描述

问题:

我想从点击的 td 中获取 reservationid 到我的 Bootstrap 模式中。

JS代码:

$('#reservationtable tbody td').on('click', function () {
        if($(this).hasClass("reserved") || $(this).hasClass("reserved-right")){
            var reservationid = $(this).attr('id');

            $.ajax({
                cache: false,
                type: 'POST',
                url: 'abfragereservierung.php',
                data: 'reservationid='+reservationid,
                success: function(data)
                {
                    $('#abfrageresrvierung').show();
                }
            });
        }
    });

HTML 代码:

<td class="reserved" data-id="'. $counter['1']->reservationid .'">
<td class="reserved" data-id="'. $counter['2']->reservationid .'">
<td class="reserved" data-id="'. $counter['3']->reservationid .'">

$counter 变量的内部对于每个键 (1,2,3) 都有一个类型为 Reservation 的对象

引导模式:

<?php

session_start();

require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Models/User.php");
require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Controllers/ReservierungsController.php");
require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Controllers/UserController.php");
require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Models/Reservierung.php");

$resController = new ReservierungsController();

//PLACE WHERE I WANT TO GET THE RESERVATIONID

?>

    <div class="modal fade" id="abfragereservierung" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        .....

更新:

现在我看到一个 POST 请求,其中包含正确的 reservationid。但我不能通过 $_POST['reservationid'] 使用它

模态:

 <?php

session_start();

require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Models/User.php");
require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Controllers/ReservierungsController.php");
require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Controllers/UserController.php");
require_once("C:/xampp/htdocs/platzverwaltungssystemprotoyp/app/Models/Reservierung.php");

$userController = new UserController();
$resController = new ReservierungsController();
if($_POST['reservationid'] != ""){
    $reservierung = $resController->getReservierungFromId($_POST['reservationid']);
}

?>

<form method="post">
    <div class="modal fade" id="abfragereservierung" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Reservierung Nr. <?php echo $reservierung->reservationid ?></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <h6>Daten</h6>
                    <div id="reservationdata">
                        <table class="table table-hover">
                            <tbody>
                            <tr>
                                <th>Name</th>
                                <td><p><?php echo $userController->getUserFromId($_POST['reservationid'])->nachname ?></p></td>
                            </tr>
                            <tr>
                                <th>Platz</th>
                                <td><p><?php echo $reservierung->tenniscourts_tenniscourtid ?></p></td>
                            </tr>
                            <tr>
                                <th>Datum</th>
                                <td><p><?php echo $reservierung->reservierung_am ?></p></td>
                            </tr>
                            <tr>
                                <th>Uhrzeit von</th>
                                <td><p><?php echo $reservierung->reservierungsanfang ?></p></td>
                            </tr>
                            <tr>
                                <th>Uhrzeit bis</th>
                                <td><p><?php echo $reservierung->reservierungsende ?></p></td>
                            </tr>
                            <tr>
                                <th>Bemerkung</th>
                                <td><p><?php echo $reservierung->bemerkung ?></p></td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Abbrechen</button>
                    <button class="btn btn-warning" type="submit" name="submit" id="submit" value="submit">Reservierung stornieren</button>
                </div>
            </div>
        </div>
    </div>
</form>

标签: phpjquerybootstrap-4

解决方案


它应该是var reservationid = $(this).attr('data-id');


推荐阅读