如何将行中的ID从数据库传递到模式对话框?

时间:2015-08-23 23:24:36

标签: php mysqli modal-dialog

我有一个模态对话框,我希望它根据特定行的ID显示数据库中某行的特定字段。我试图将行ID从数据库传递到模态对话框,以便我可以将它用于查询但我有挑战,我该如何完成?

 <?php

    $result = mysqli_query($con,"SELECT * FROM general_reservation ");
    $count = mysqli_num_rows($result);
echo "<b><center><h3>Cars Reserved By Districts</h3></center></b><br><br>";

while($row=mysqli_fetch_array($result)){
    $reservation_id = $row['reservation_id'];
    ?>

    <tr>
         <td><a  name="reserver"  data-id="myModal"  class="btn btn-link" data-toggle="modal" href="#myModal" >Reserver's Profile</a></td>

    </tr>

    <?php
$reserver = $row['reservation_id'];
    } //end of while loop ?>

    </tbody>
</table> 
<div class="modal fade" id="myModal" role="dialog">
             <div class="modal-dialog modal-sm">
             <div class="modal-content">
             <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><center>Reserver</center></h4>
            </div>
            <?php 
            $reserver = $row['reservation_id'];

        $reservation_id = 'reservation_id' ;
        $result = mysqli_query($con,"SELECT * FROM general_reservation WHERE reservation_id = '$reserver' ");
        $count = mysqli_num_rows($result);
        while($row=mysqli_fetch_array($result)){

                ?>
        <div class="modal-body">
        <p ><strong>First Name: </strong><?php echo $row['fname'];?> </p><br>
        <p ><strong>Last Name: </strong><?php echo $row['lname'];?></p><br>
        <p ><strong>Mobile Number: </strong><?php echo $row['mobile_number'];?></p>
        </div>
        <?php } //end of while loop ?>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div> 
  </div>

1 个答案:

答案 0 :(得分:2)

我有同样的问题,我曾尝试过jQuery对话框,许多已知和未知的弹出插件甚至从codecanyon支付了一个,但没有像bootstrap模式一样开箱即用,每个弹出插件都必须编写自定义代码才能在PHP内部工作while循环然后下一个问题是使用新内容刷新模态而不刷新页面。

我使用bootstrap模式的方法是我在你的案例reservation-profile.php中创建了一个单独的php文件,并在href链接中添加id并调用弹出模式。

<?php
    $result = mysqli_query($con,"SELECT * FROM general_reservation ");
    $count = mysqli_num_rows($result);
    echo "<b><center><h3>Cars Reserved By Districts</h3></center></b><br><br>";

    while($row=mysqli_fetch_array($result)){
        $reservation_id = $row['reservation_id'];
    ?>

    <tr><td>
     <a data-toggle="modal" href="reservation-profile.php?id=<?php echo $reservation_id;?>" data-target="#myModal" class="btn btn-link">Reserver's Profile</a>
    </td></tr>

<?php
    //$reserver = $row['reservation_id']; //Don't Need this
    } //end of while loop
?>

//Bootstrap Modal
<div class="modal fade" id="myModal">
     <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <strong>Loading...</strong>
        </div>
    </div>
</div>

现在创建一个像这样的reservation-profile.php文件

<?php
//Include database connection here
$reserver = $_GET["id"]; //escape the string if you like
$result = mysqli_query($con,"SELECT * FROM general_reservation WHERE reservation_id = '$reserver' ");
//$count = mysqli_num_rows($result); //Don't need to count the rows too
$row = mysqli_fetch_array($result); //Don't need the loop if you wana fetch only single row against id
?>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title"><center>Reserver</center></h4>
</div>
<div class="modal-body">
    <p ><strong>First Name: </strong><?php echo $row['fname'];?> </p><br>
    <p ><strong>Last Name: </strong><?php echo $row['lname'];?></p><br>
    <p ><strong>Mobile Number: </strong><?php echo $row['mobile_number'];?></p>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>

最后也是最重要的部分是在没有页面刷新的情况下刷新具有新细节的模态。

按照一段代码完成工作。你可以阅读更多相关信息here
注意:将以下代码添加到您调用模式的页面。请勿在{{1​​}}

中添加此内容
reservation-profile.php

因此,整个解决方案开箱即用,无需编写任何额外的代码行。