【问题标题】:jQuery AJAX variable sent through a POST request but I can't use it in PHP通过 POST 请求发送的 jQuery AJAX 变量,但我不能在 PHP 中使用它
【发布时间】:2021-01-14 19:21:21
【问题描述】:

目标:

如果我点击td,则会发出一个jQuery AJAX 请求,该请求通过POST 请求将正确的reservationid 发送到td 所在的同一页面。在 PHP 文件中,我想调用一个查询并使用通过 POST 发送的 id 获取有关预订的信息。之后,它会打开一个 Bootstrap 模式,其中显示有关单击的预订的预订详细信息。

问题:

在网络视野中,我可以看到一个 POST 请求被触发并发送了 reservationid。但是我不能使用$_POST['reservationid'],PHP 说它没有任何价值。如果我尝试使用var_dump($_POST),那么其中也没有任何价值。

if ($(this).hasClass("reserved") || $(this).hasClass("reserved-right")) {
  var reservationid = $(this).attr('data-id');

  $.ajax({
    type: 'POST',
    url: 'http://localhost/platzverwaltungssystemprotoyp/app/Views/reservierung/platzverwaltung.php',
    data: {
      'reservationid': reservationid
    },
    success: function() {
      $('#abfragereservierung').find('#reservationid').val(reservationid);
      $("#abfragereservierung").modal("show");
    }
  });
<td class="reserved" data-id="'. $counter['1']->reservationid .'"></td>
<td class="reserved" data-id="'. $counter['2']->reservationid .'"></td>
<td class="reserved" data-id="'. $counter['3']->reservationid .'"></td>

<?php
  include "modals/abfragereservierung.php"
?>

abfragereservierung.php:

<?php
  $reservationIdFromTd = isset($_POST['reservationid']) ? $_POST['reservationid'] : null;
  if ($reservationIdFromTd != null) {
    $reservierungFromId = $resController->getReservierungFromId($reservationIdFromTd);
  }    
?>

<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 $reservierungFromId->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>
                <input type="text" name="reservationid" id="reservationid">
                <tr>
                  <th>Name</th>
                  <td>
                    <p>
                      <?php echo $_POST['reservationid']; ?>
                    </p>
                  </td>
                </tr>
                <tr>
                  <th>Platz</th>
                  <td>
                    <p>
                      <?php echo $reservierungFromId->tenniscourts_tenniscourtid ?>
                    </p>
                  </td>
                </tr>
                <tr>
                  <th>Datum</th>
                  <td>
                    <p>
                      <?php echo $reservierungFromId->reservierung_am ?>
                    </p>
                  </td>
                </tr>
                <tr>
                  <th>Uhrzeit von</th>
                  <td>
                    <p>
                      <?php echo $reservierungFromId->reservierungsanfang ?>
                    </p>
                  </td>
                </tr>
                <tr>
                  <th>Uhrzeit bis</th>
                  <td>
                    <p>
                      <?php echo $reservierungFromId->reservierungsende ?>
                    </p>
                  </td>
                </tr>
                <tr>
                  <th>Bemerkung</th>
                  <td>
                    <p>
                      <?php echo $reservierungFromId->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>

【问题讨论】:

  • 请解释清楚你的问题是什么。

标签: php jquery ajax bootstrap-4


【解决方案1】:

在 HTML 中:

<td class="reserved" data-id="'. $counter['1']->reservationid .'"></td>
<td class="reserved" data-id="'. $counter['2']->reservationid .'"></td>
<td class="reserved" data-id="'. $counter['3']->reservationid .'"></td>
<script>
if($(this).hasClass("reserved") || $(this).hasClass("reserved-right")){
            var reservationid = $(this).attr('data-id');

            $.ajax({
                type: 'POST',
                url: 'http://localhost/platzverwaltungssystemprotoyp/app/Views/reservierung/platzverwaltung.php',
                data: {
                    'reservationid': reservationid
                },
                success: function (){
                    $('#abfragereservierung').find('#reservationid').val( reservationid );
                    $("#abfragereservierung").modal("show");
                }
            });
</script>

在 abfragereservierung.php 中:

<?php
$reservationIdFromTd = isset($_POST['reservationid']) ? $_POST['reservationid'] : null;

if($reservationIdFromTd != null){
    $reservierungFromId = $resController->getReservierungFromId($reservationIdFromTd);
}

?>

<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 $reservierungFromId->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>
                            <input type="text" name="reservationid" id="reservationid">
                            <tr>
                                <th>Name</th>
                                <td><p><?php echo $_POST['reservationid']; ?></p></td>
                            </tr>
                            <tr>
                                <th>Platz</th>
                                <td><p><?php echo $reservierungFromId->tenniscourts_tenniscourtid ?></p></td>
                            </tr>
                            <tr>
                                <th>Datum</th>
                                <td><p><?php echo $reservierungFromId->reservierung_am ?></p></td>
                            </tr>
                            <tr>
                                <th>Uhrzeit von</th>
                                <td><p><?php echo $reservierungFromId->reservierungsanfang ?></p></td>
                            </tr>
                            <tr>
                                <th>Uhrzeit bis</th>
                                <td><p><?php echo $reservierungFromId->reservierungsende ?></p></td>
                            </tr>
                            <tr>
                                <th>Bemerkung</th>
                                <td><p><?php echo $reservierungFromId->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>

不要将 PHP 包含到 HTML,因为它是 PHP 加载的,它会加载模态的,它应该在 Ajax 中加载

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 2014-05-09
    • 2011-06-13
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    相关资源
    最近更新 更多