你如何只在按下按钮运行PHP帖子

时间:2017-11-24 20:08:29

标签: php

每当我刷新页面时,即使我没有按下按钮,模态也会自动打开。我的if语句中是否有一种方法可以在按下按钮时仅打开它?谢谢!

我是PHP的新手,所以我的代码效率可能不高。

<?php
                            include_once 'includes/database.php';
                            if ($_SESSION['renterID'] == 0){
                              $sql = "SELECT firstName, lastName, paymentID, paymentAmount, paymentDate, paymentPaid FROM renter, payment WHERE renter.renterID = payment.renterID ORDER BY paymentDate ASC";

                            } else {
                              $sql = "SELECT firstName, lastName, paymentID, paymentAmount, paymentDate, paymentPaid FROM renter, payment WHERE renter.renterID = payment.renterID AND $_SESSION[renterID] = renter.renterID";
                            }
                            $result = mysqli_query($conn, $sql);
                            $resultCheck = mysqli_num_rows($result);
                            if ($resultCheck > 0) {
                              while($row = $result->fetch_assoc()) {
                                echo
                                "<tr>
                                    <td>$row[paymentID]</td>
                                    <td>$row[firstName] $row[lastName]</td>
                                    <td>$row[paymentAmount]</td>
                                    <td>";echo date ('F d, Y', strtotime($row['paymentDate'])); echo "</td>";
                                    if ($_SESSION['renterID'] == 0) {
                                      if ($row['paymentPaid'] == '0') {
                                        echo "<td class='text-center'><a class='btn btn-danger btn-sm'><i class='fa fa-times' aria-hidden='true'>Paid</i></a></td>";
                                      } else {
                                        echo "<td class='text-center'><a class='btn btn-success btn-sm'><i class='fa fa-check' aria-hidden='true'>Paid</i></a></td>";
                                      }
                                    } else {
                                    if ($row['paymentPaid'] == '0'){
                                      echo "

                                          <td class='text-center'><button href='#' class='btn btn-success btn-sm' id='payNowButton' name='payNowID' value='$row[paymentAmount]'><i class='fa fa-usd' aria-hidden='true'>Pay Now</i></button></td>

                                      ";
                                    } else {
                                      echo "<td class='text-center'><a class='btn btn-sm'><i class='fa fa-check' aria-hidden='true'>Paid</i></a></td>";
                                    }
                                  }
                                echo "</tr>";
                              }
                            } else {
                              echo "<tr><td colspan='5'>No payments at this time</td></tr>";
                            }


    <?php
  if(isset($_POST['payNowID'])){
    $totalAmount = $_POST['payNowID'];

    echo "<script>
    $(document).ready(function() {
      $('#payNowButton').click(function() {
        $('#pay_now').modal('show');
      });
    });
    </script>";
  }
?>

1 个答案:

答案 0 :(得分:0)

这不是一个php问题,而是一个jQuery问题。你在jQuery中设置它必须在文档就绪时显示模态。那是因为它出现了。 用以下代码替换jQuery方法:

$(document).ready(function() {
 $('.btn').click(function() {
   $('#pay_now').modal('show');
  });
});

这样,只有单击按钮才会显示模态。