【问题标题】:pass id from database to open modal将 id 从数据库传递到打开模式
【发布时间】:2017-05-19 17:52:22
【问题描述】:

这是代码,其中所有优惠券表数据都显示有获取代码按钮。我无法发送选定的优惠券 ID 以打开模式。她是成功显示所有数据的代码。下面是模式代码,我必须在其中传递优惠券 ID。

代码完美运行..

  <?php
 $q=mysqli_query($con,"  SELECT c.* , sc.* , sm.* ,ca.* from store_category sc INNER JOIN store_manufacture sm ON sm.sm_id=sc.store_id INNER JOIN categories ca ON ca.cat_id=sc.cat_id INNER JOIN coupons c on c.c_sc_id=sc.sc_id ");
 while($row1=mysqli_fetch_array($q,MYSQLI_ASSOC)) {
  $h = strpos($row1['sm_link'],'http');
 ?>
 <div id="popular" class="tab-pane counties-pane active animated fadeIn">
 <div class="coupon-wrapper row">
 <div class="coupon-data col-sm-2 text-center">
  <div class="savings text-center">
  <div>
  <div class="large"><?php echo $row1['c_name'] ?></div>
  <div class="type"><?php echo $row1['sm_brand_name'] ?></div>
   </div> </div> </div>
  <div class="coupon-contain col-sm-7">
  <h4 class="coupon-title"><a href="<?php echo ($h !== FALSE)?$row1['sm_link']:"http://".$row1['sm_link'] ?>" target="_blank"><?php echo $row1['c_description']?></a></h4>
   <p data-toggle="collapse" data-target="#1">Shop these Shopname deals of the day to save as much...</p>
   <p id="1" class="collapse">Don't miss out on all the coupon savings.Get you coupon now and save big</p>
     </div>
    <div class="button-contain col-sm-3 text-center">
    <p class="btn-code" data-toggle="modal" data-target=".couponModal">
    <span class="partial-code"><?php echo $row1['c_code'] ?></span> 
    <span class="btn-hover">Get Code</span></p>
    </div></div> </div>
      <?php
           }
       ?>

模态在同一页面上。

<?php
 // can't get the query for the selected coupon code to open this modAL
?>
  <div class="coupon_modal modal fade couponModal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
  <div class="modal-content">
  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
 <span aria-hidden="true"><i class="ti-close"></i></span></button>
 <div class="coupon_modal_content">
 <div class="row">
  <div class="col-sm-10 col-sm-offset-1 text-center">
  <h2><?php echo $row1['c_name'] ?></h2>
  <p><?php echo $row1['c_description'] ?></p>
  </div><div class="row">
  <div class="col-sm-12"><h5 class="text-center text-uppercase m-t-20 text-muted">Click below to get your coupon code</h5></div>
  <div class="col-sm-4 col-sm-offset-4 col-xs-6 col-xs-offset-3">
  <a href="#" target="_blank" class="coupon_code alert alert-info"><span class="coupon_icon">
   <i class="ti-cut hidden-xs"></i></span> <?php echo $row1['c_code'] ?> </a></div></div> </div></div>
                        <!-- end: Coupon modal content -->
  </div></div></div> </div> <!-- end: Modall --></div>
        <?php
        }
        ?>

【问题讨论】:

    标签: javascript php jquery twitter-bootstrap mysqli


    【解决方案1】:

    只需将要转移的 ID 附加到优惠券模式名称即可。

    <p class="btn-code" data-toggle="modal" data-target=".couponModal_<?php echo $your_id; ?>">
    

    最后,在 Modal 中,使用下面的代码。

    <div class="coupon_modal modal fade couponModal_<?php echo $your_id; ?>" tabindex="-1" role="dialog">
    

    希望这行得通。


    由于您的数据是在 PHP 的 while 循环中填充的,因此您需要在循环中包含整个模式代码。

    澄清一下,由于您的数据是从数据库中填充的,因此强烈建议对 $your_id 进行加密以确保其安全。

    【讨论】:

    • 我已经编辑了我的答案以进一步解释这一点。为了能够在 Modal 中捕获不同记录的不同值,您需要有不同的 modals。这可以通过在 PHP while 循环中包含模式代码并如前所述附加 $your_id 来轻松实现。
    • 小心使用class打开模态
    • 通过适当的教育完成工作和在没有适当教育的情况下完成工作是两件不同的事情。我们使用IDs 来区分同一类别的不同事物。如果您有一门课程要学习,并且有 30 名学生怎么办。将创建 30 个部分或只为所有 30 个学生提供唯一 ID。想想吧,伙计
    • 请注意 $your_id 实际上是 $row['c_id']。我更喜欢将获取的值存储在一个变量中,然后使用。
    • @MohammedAkhtarZuberi 我也喜欢这个,但这是另一个问题。
    【解决方案2】:

    对 Akhtar 的回答稍作修改。我会推荐使用ID

    <p class="btn-code" data-toggle="modal" data-target="#couponModal_<?php echo $row1['c_id'] ?>">
    

    最后,在 Modal 中,使用下面的代码。

    <div class="coupon_modal modal fade" id="couponModal_<?php echo $row1['c_id'] ?>"  tabindex="-1" role="dialog">
    

    //完整代码

    <?php
     $q=mysqli_query($con,"  SELECT c.* , sc.* , sm.* ,ca.* from store_category sc INNER JOIN store_manufacture sm ON sm.sm_id=sc.store_id INNER JOIN categories ca ON ca.cat_id=sc.cat_id INNER JOIN coupons c on c.c_sc_id=sc.sc_id ");
     while($row1=mysqli_fetch_array($q,MYSQLI_ASSOC)) {
      $h = strpos($row1['sm_link'],'http');
     ?>
    <p class="btn-code" data-toggle="modal" data-target="#couponModal_<?php echo $row1['c_id'] ?>">
     <div id="popular" class="tab-pane counties-pane active animated fadeIn">
     <div class="coupon-wrapper row">
     <div class="coupon-data col-sm-2 text-center">
      <div class="savings text-center">
      <div>
      <div class="large"><?php echo $row1['c_name'] ?></div>
      <div class="type"><?php echo $row1['sm_brand_name'] ?></div>
       </div> </div> </div>
      <div class="coupon-contain col-sm-7">
      <h4 class="coupon-title"><a href="<?php echo ($h !== FALSE)?$row1['sm_link']:"http://".$row1['sm_link'] ?>" target="_blank"><?php echo $row1['c_description']?></a></h4>
       <p data-toggle="collapse" data-target="#1">Shop these Shopname deals of the day to save as much...</p>
       <p id="1" class="collapse">Don't miss out on all the coupon savings.Get you coupon now and save big</p>
         </div>
        <div class="button-contain col-sm-3 text-center">
        <p class="btn-code" data-toggle="modal" data-target=".couponModal">
        <span class="partial-code"><?php echo $row1['c_code'] ?></span> 
        <span class="btn-hover">Get Code</span></p>
        </div></div> </div>
    
    <div class="coupon_modal modal fade couponModal" tabindex="-1" role="dialog" id="couponModal_<?php echo $row1['c_id'] ?>">
      <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
     <span aria-hidden="true"><i class="ti-close"></i></span></button>
     <div class="coupon_modal_content">
     <div class="row">
      <div class="col-sm-10 col-sm-offset-1 text-center">
      <h2><?php echo $row1['c_name'] ?></h2>
      <p><?php echo $row1['c_description'] ?></p>
      </div><div class="row">
      <div class="col-sm-12"><h5 class="text-center text-uppercase m-t-20 text-muted">Click below to get your coupon code</h5></div>
      <div class="col-sm-4 col-sm-offset-4 col-xs-6 col-xs-offset-3">
      <a href="#" target="_blank" class="coupon_code alert alert-info"><span class="coupon_icon">
       <i class="ti-cut hidden-xs"></i></span> <?php echo $row1['c_code'] ?> </a></div></div> </div></div>
                            <!-- end: Coupon modal content -->
      </div></div></div> </div> <!-- end: Modall --></div>
          <?php
               }
           ?>
    

    【讨论】:

    • 这个正在工作,但它正在以褪色屏幕的形式打开模式。
    • 这个对我有用,但模态显示仍然褪色
    • 检查你的 html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2016-11-11
    • 2021-05-26
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多