dongqian6554 2018-04-08 23:01
浏览 155
已采纳

如何使用GET方法将id传递给同一页面中的模态?

I have a following link:

<a type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#product_view" href="?id='.$row['upcoming_event_id'].'">
                                    <i class="fa fa-search"></i> Read more</a>

The read more looks like this:

enter image description here

When I press the read more button, a modal will pop up like this:

enter image description here

I tried the following code but it's not working:

<div class="modal fade product_view" id="product_view">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <a href="#" data-dismiss="modal" class="class pull-right"><span
                            class="glyphicon glyphicon-remove"></span></a>
                <h3 class="modal-title">I want to show the event id here <?php echo $_GET['id']; ?></h3>
            </div>

            </div>
        </div>
    </div>
</div>
</div>

How to solve this? Or is there any better way to do this? I have to show the id into the modal, not on another page. Any help will be appreciated. Thanks in advance.

  • 写回答

3条回答 默认 最新

  • doudao3170 2018-04-08 23:14
    关注

    PHP code execute first, so you can't set PHP value for $_GET dynamically. Use a javascript function.

    function setEventId(event_id){
        document.querySelector("#event_id").innerHTML = event_id;
    }
    

    Call setEventId() function from anchor tag

    <a type="button" onclick="setEventId(<?= $row['upcoming_event_id'] ?>)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
    

    And use <span id="event_id"></span> to replace html value with event_id

    <div class="modal fade product_view" id="product_view">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <a href="#" data-dismiss="modal" class="class pull-right"><span
                                class="glyphicon glyphicon-remove"></span></a>
                    <h3 class="modal-title">I want to show the event id here <span id="event_id"></span></h3>
                </div>
    
                </div>
            </div>
        </div>
    </div>
    </div>
    

    Here is demo example after getting value of $row['upcoming_event_id'] or page load.

    function setEventId(event_id){
        document.querySelector("#event_id").innerHTML = event_id;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <a type="button" onclick="setEventId(1)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
                                        
    <a type="button" onclick="setEventId(2)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
                                        
                                        
    <a type="button" onclick="setEventId(3)" class="btn btn-primary" data-toggle="modal"
                                        data-target="#product_view" href="javascript:void(0)">
                                        <i class="fa fa-search"></i> Read more</a>
                                        
    <div class="modal fade product_view" id="product_view">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <a href="#" data-dismiss="modal" class="class pull-right"><span
                                class="glyphicon glyphicon-remove"></span></a>
                    <h3 class="modal-title">I want to show the event id here <span id="event_id"></span></h3>
                </div>
    
                </div>
            </div>
        </div>

    </div>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部