无法通过JQuery将PHP变量传递给模态弹出窗口

时间:2015-02-20 00:21:52

标签: php jquery ajax

有一个单页,payments.php如下所示。此页面最初包含从SQL查询填充的表。当单击表上的名称时,它应该通过SQL查询中使用的付款ID变量来填充该付款的详细信息。

付款的ID通过URL中的data-pid变量从php传递到JQuery,然后转换为JQuery变量pmntid。在这个阶段,一切运行良好,正确的pmntid显示在Firebug的控制台中,如下所示。

enter image description here

此时使用AJAX POST将pmntid变量转换为idNumber并显示弹出窗口。弹出窗口有一个回显来显示数字。当显示弹出窗口时,idnumber的回显中没有值且没有数据,查看Firebug中的Post显示idNumber等于正确的数字。执行print_r($_POST)会得到Array()的结果。

已经搜索过这个问题的答案,但却无法使其正常工作,其中的代码如下所示:

payments.php

<?php
session_start();

$pageTitle = "Payments";
require 'dbconnect.php';
include 'header.php';

//Entered Payments
$paymentProcEntered = "SELECT * FROM test";
$paymentProcQuery = $connection->query($paymentProcEntered);

mysqli_close();

?>

<!DOCTYPE html>
<html>
    <head>
    </head>
   <body>
<div id="main">

    <div id="mainContainer">

<p class="sectionTitle">Click On Name To View Details.</p>
<?php
    echo "<table class='pmntTable'>
    <tr class='pmntRowBgd'>
        <td>
    <div id='pmntContainer'>
    <div class='pmntMainTitlelft'>Payments</div>
    <div class='pmntMainTitlergt'>Total: ".$paymentProcQuery->num_rows."</div>
            <div id='pmntListingContainer'>
    <table class='sortable'>
    <thead>
    <tr>
        <th class='sorttable_alpha'>Holder</th>
        <th>Amount</th>
        <th>Names</th>
        <th>Date Received</th>
        <th>Date Entered&nbsp</th>
        <th class='sorttable_nosort'>Entered By</th>
    </tr>
    </thead>";

    while($row = mysqli_fetch_array($paymentProcQuery))
        {
        $dateReceived = date_create($row[6]);
        $dateEntered = date_create($row[7]);
        echo "<tr>";
?>

    <td class="listingTextLeft">
    <a href="?pid=<?php echo $row[0] ?>" data-pid="<?php echo $row[0] ?>" class="pmntDetail">
    <?php echo $row[20] ?></a></td>
<?php
echo "<td class='listingTextRight'> $".number_format($row[4],2)."</td>
<td class='listingTextCentre'>".number_format($row[5])."</td>
<td class='listingTextCentre'>".date_format($dateReceived,"d-M-Y")."</td>
<td class='listingTextCentre'>".date_format($dateEntered,"d-M-Y")."</td>
<td class='listingTextCentre'>".$row[8]."</td>
</tr>";
    }
    echo "</table>
        </div>
        </div>
    </td>
    </tr>
</table>
</div>";    
?>

<div id="pmntDetailPopup" class="pmnt_content">

<?php 

include 'dbconnect.php';

$idNumber = $_POST['idNumber'];
echo "ID: ".$idNumber;

$paymentDetailEntered = "SELECT * FROM test WHERE paymentid = '$idNumber'";
$paymentDetailQuery = $connection->query($paymentDetailEntered);

mysqli_close();

while($row = mysqli_fetch_array($paymentDetailQuery))
    {
    $dateReceived = date_create($row[6]);
    $dateEntered = date_create($row[7]);
    $dateCABEnter = date_create($row[10]);
    $dateCABCheck = date_create($row[13]);
    $dateDetailCheck = date_create($row[17]); 
echo "<div class='sectionTitle'>Payment Details</div>
    <div id='pmntPopupContainer'>
    <div class='sectionSubtitleLeft pmntSubTitlelft'>Initial Entry</div>
    <ul class='pmntDetailList'>
    <li><label>Holder</label><span>".$row[20]."</span></li>
    <li><label>Amount</label><span> $".number_format($row[4],2)."</span>
    <li><label>Number Of Names</label><span>".number_format($row[5])."</span></li>
    <li><label>Date Received</label><span>".date_format($dateReceived,"d-M-Y")."</span></li>
    <li><label>Date Entered In Database</label><span>".date_format($dateEntered,"d-M-Y")."</span></li>
    <li><label>Entered By</label><span>".$row[8]."</span></li>
    </ul>
    </div>
    </div>
</div>";
                }
?>

<script>
jQuery('.pmntDetail').each(function(i,v){
    jQuery(v).click(function(paymentID){
        paymentID.preventDefault();
        paymentID.stopPropagation();
        var pmntid = $(this).data("pid");
     console.log("ID: ", pmntid);
        $.ajax({
            type: 'post',
            url: 'payments.php',
            data: { idNumber: pmntid },
                success: function(data) {
                    $("#pmntDetailPopup").modal({position: ["5%"]});    
                }
        });
    });
});
</script>
  </body>
</html>

我已经使用idNumber的静态值测试了弹出窗口,它可以正常工作,错误只是将变量传递给查询以填充弹出窗口。

任何帮助都会受到赞赏,我希望我已经解释得很好。

0 个答案:

没有答案