如何将javascript变量传递给模态

时间:2017-03-08 09:20:20

标签: javascript php jquery html css

<div>
    <h5>
        <a href="<?php echo base_url(); ?>/vendor/home/projects" >Back to Projects</a>
        <h5>
            <div>
                <div class="container">
                    <div class="row">

                        <?php
                            foreach ($projects as $value) {
                        ?>

                            <?php  
                                $project_images =  json_decode($value['project_gallery'],true);
                                $i=0; 

                                foreach ($project_images as $project_image_value) {
                                    $i++;
                                ?>  

                                    <div class="col-md-2">
                                        <img src="<?= base_url() ?>assets/uploads/projects/<?=$project_image_value['fname']?>" onclick="openModal(<?= $i?>)"   class="img-thumbnail img-responsive " />

                                        <div><?=$project_image_value['title']?></div>
                                    </div>
                                <?php

                                }

                                ?>
                        <?php
                            }
                        ?>
                    </div>
                </div>

这里我将id值传递给javascript

<script>
    function openModal(id) {
        $("#img_id").val(id);
        document.getElementById('myModal').style.display = "block";
        document.getElementById('caption').innerHTML = img_id;
    }

现在我想在php代码中使用模态中的id

<div id="myModal" class="modal">

<button type="button" id="close"  onclick="closeModal()" >close</button>

<input type="hidden" id="img_id" name="img_id">

<div class="modal-content">

<div class="mySlides">

 <?php    

 $id=0; 

foreach ($project_images as $project_image_value) {

 $id++;

//echo $project_image_value[title];

?>                                                

<img src="<?= base_url() ?>assets/uploads/projects/<?=$project_image_value['fname']?>" style="width:100%">

<div class="caption-container">

<p id="caption"> <?php echo $project_image_value[title];?></p>

<?php

}   

 ?>

</div>

</div>

</div>

</div>

所有代码都在同一个页面中。我可以将该id从javascript传递给模态,以便我将在php代码中使用。是可能的。请帮助我

2 个答案:

答案 0 :(得分:0)

你有设定值的拼写错误到模态。您正在为输入分配img_id。这不是价值。改变你的脚本,如:

<script>

function openModal(id) {

$("#img_id").val(id);

document.getElementById('myModal').style.display = "block";

document.getElementById('caption').innerHTML = id; //<-- change this line


}

答案 1 :(得分:0)

您可以通过GET刷新网址传递变量,如:

www.something.com?attr=some

然后通过$ _SERVER ['PHP_SELF']在php中读取这些数据并将其爆炸。