douren7921 2017-11-13 06:21
浏览 51
已采纳

如何将变量发送到模态以显示从数据库php查询的值

I am trying to send value to modal to query in table from database and display the value that come from WHERE clause that sent from variable I assign it to modal with 'id' attribute I try to save it with javascript in cookie but it show random value and not working correct

here is the link that i click on it to show modal it is in loop in table

<td>
    <a data-toggle="modal" href="#gurantorname" id="'.$payment_row["GuarantorID"].'"> Guarantor Discount</a>
</td>

and here is my modal :

<!-- gurarntour modal-->
<div id="gurantorname" class="modal fade" tabindex="-1" data-width="400">
    <div class="modal-header red">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h4 class="modal-title"> اسم الكفيل  </h4>
    </div>
    <div class="modal-body">
        <?php
        if(isset($_COOKIE['myJavascriptVar'])) { 
            $_COOKIE['myJavascriptVar'];
            echo $_COOKIE['myJavascriptVar'];
            $guarantor_sth = $guarantorobj->getguarantorapplicationbyid($_COOKIE['myJavascriptVar']);
            $guarantor_row = $guarantor_sth->fetch();
        ?>
        <label class=" font-blue-dark text-center">
            <strong>
            <?php
                echo $guarantor_row["GuarantorFirstNameAR"].' '.$guarantor_row["GuarantorSecondNameAR"] .' '.$guarantor_row["GuarantorThirdNameAR"].' '.$guarantor_row["GuarantorLastNameAR"] ;
            ?>
            </strong>
        </label>
        <?php  
        }
        ?>
    </div>
    <div class="modal-footer text-right">
        <a target="_blank"  class="btn green" href="index.php?action=guarantorinfo&guarantorid=<?php echo $payment_row["GuarantorID"];?>" >معلومات الكفيل</a>
        <button type="submit" data-dismiss="modal" class="btn btn-outline dark" >اخفاء</button>
    </div>
</div>
<!--end of gurarntour modal-->

and here is my javascript that send cookie to modal :

<script type="text/javascript">
    $('#gurantorname').on('show.bs.modal', function(e) {
        var $modal = $(this),
        esseyId = e.relatedTarget.id;
        document.cookie = "myJavascriptVar ="+esseyId ;
        alert(esseyId)
    });
</script>
  • 写回答

1条回答 默认 最新

  • dtd14883 2017-11-13 07:06
    关注

    I think you should using ajax when user clicks on a link

    <a href="javascript:void(0)" class="link" id="'.$payment_row["GuarantorID"].'"> Guarantor Discount</a>
    

    and add the id attribute to label for showing guarantor_row by $payment_row["GuarantorID"]

    <label id="guarantor_row" class=" font-blue-dark text-center">
    

    let's create the ajax script

     $('body').on('click','.link',function(){
        var id = $(this).attr('id');
        $.ajax({
            method: "POST",
            url: "getguarantorapplicationbyid.php",
            data: { id:id  }
        })
        .done(function( json_response) {
            var guarantor_row = $.parseJSON(json_response);             
            //set the data to modal and display
            //guarantor_row.GuarantorFirstNameAR
            //guarantor_row.GuarantorSecondNameAR
            $('#guarantor_row').text(guarantor_row.GuarantorFirstNameAR+" "+guarantor_row.GuarantorSecondNameAR);
            $('#gurantorname').modal('show');
    
    
        });
     });
    

    getguarantorapplicationbyid.php

    $guarantor_sth = $guarantorobj->getguarantorapplicationbyid($_POST['id']);
    $guarantor_row = $guarantor_sth->fetch();
    echo json_encode(guarantor_row);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line