0
<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>

Here i am passing id value to javascript

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

now i want use that id in modal in php code

<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>

All the code is in the same page.how can i pass that id from javascript to modal so that i will use in the php code.Is it Possible.please help me

EddyTheDove
  • 12,979
  • 2
  • 37
  • 45
Kalyan Kumar
  • 3
  • 1
  • 6
  • I think it is already set in the img_id input. Whenever you click an image you change the img_id input by setting the new id as its value and then the modal opens. – Ghassen Rjab Mar 08 '17 at 09:31
  • yes but how can i grab that img_id value to use in the php code written in modal.thats my problem. – Kalyan Kumar Mar 08 '17 at 09:33
  • What sort of treatment you want to do? – Ghassen Rjab Mar 08 '17 at 09:40
  • I want to use the img_id with if condition in forloop so that when click on image,that image has to zoom.Actually my aim is when i click on image,that image should zoom as like light box – Kalyan Kumar Mar 08 '17 at 09:43
  • You can achieve this behavior by using javascript to do the testing when the modal opens or by using remote modal and passing the img_id as a get parameter in the url of the remote modal. check this link http://stackoverflow.com/questions/18378720/bootstrap-3-with-remote-modal to see how remote modal works. – Ghassen Rjab Mar 08 '17 at 09:51
  • sorry.I am not able to understand – Kalyan Kumar Mar 08 '17 at 10:25
  • You can create your bootstrap modal in a different action in your controller and call this modal using its url. It is pretty simple. – Ghassen Rjab Mar 08 '17 at 13:52

2 Answers2

0

You have typo in set value to modal. You are assigning img_id to input. which is not the value. change your script like:

<script>

function openModal(id) {

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

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

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


}
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • My problem is i want to use the id in php code written in modal which is passed to javascript.how to pass the id from javascript to modal written in same page to use in php code – Kalyan Kumar Mar 08 '17 at 09:51
0

You can refresh the page passing variables in url by GET like:

www.something.com?attr=some

then read in php these data by $_SERVER['PHP_SELF'] and explode them.

Jortega
  • 51
  • 6