【问题标题】:Pass PHP variable with key to Bootstrap Modal将带有键的 PHP 变量传递给 Bootstrap 模式
【发布时间】:2021-01-13 20:52:54
【问题描述】:

问题:

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

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>

【问题讨论】:

  • 它是一个 data-id 属性 var reservationid = $(this).attr('data-id');
  • 你得到什么错误? reservationid 中包含的值是什么?
  • 或者你可以用.data('id')代替.attr('data-id')
  • @executable 有预订的 id 像 '23', '54', ...in reservationid
  • @HassanKalhoro 但如果成功,模式会打开对吗?因为打不开

标签: php jquery bootstrap-4


【解决方案1】:

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

【讨论】:

  • 另外,你可以使用 $(this).data("id") 代替那个
  • 是的。您可以使用两者中的任何一个
  • @RayCaballero 感谢您的更正,但如果 ajax 成功,它会打开模式对吗?因为模态不打开
  • @Gulaschsuppe123,根据您的代码,如果成功,模式应该打开。检查您的 AJAX 或在此处发布有关它的其他问题,以便我们为您提供帮助。
  • 请记住,.data().attr() 存在差异,预期结果可能是 confusing when you mix them up
猜你喜欢
  • 2016-04-14
  • 2023-03-18
  • 2014-06-21
  • 1970-01-01
  • 2018-09-28
  • 2020-01-24
  • 2020-01-17
  • 2020-08-03
相关资源
最近更新 更多