0

I'm trying to implement a modal window that is within a PHP page on a Single Page Application website, and I haven't been able to get the modal window to work.

Even though I have a .js file where I'm storing my functions, I have the javascript within the PHP page where I'm trying to have the modal window working. I would prefer all the functions to be in the .js file, but I could not get this to work either way.

The PHP page

This is not index.php, but is called after the initial page load

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div id="myModal" class="modal">

    <!-- Modal content -->
        <div class="modal-content">
        <span class="close">&times;</span>
        <img id="popup_img" src="#">
         </div>

    </div>
    <div id="screenings">
        <?php
            $db = mysqli_connect("localhost", "root", "", "data");
            $sql = "SELECT * FROM screenings";
            $result = mysqli_query($db, $sql);
            while ($row = mysqli_fetch_array($result)){
                echo "<div id='img_div'>";
                    echo "<img id='img_screenings' class='modal_img' src='images/".$row['image']."' >";
                    //echo "<p>" .$row['text']."</p>";
                echo "</div>";
            }
        ?>
    </div>
    <script>
    // Get the modal
    var modal = document.getElementById('myModal');

   // Get the button that opens the modal
   //var btn = document.getElementsByClassName("modal_img");

   // Get the <span> element that closes the modal
   var span = document.getElementsByClassName("close")[0];

   $("img").on("click", function() {
    var source = ( $(this).attr("src") );
   //alert(source);
  //$('#modal_popup').text(source);
  $('#popup_img').prop('src', this.src);
  });

  // When the user clicks the button, open the modal 
  document.getElementById("img_screenings").onclick = function(){
  modal.style.display = "block";
   alert("hello");
   }

   // When the user clicks on <span> (x), close the modal
   span.onclick = function() {
    modal.style.display = "none";
    }

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
     if (event.target == modal) {
    modal.style.display = "none";
    }
    }
    </script>
    </body>

The CSS for the Modal Window

I don't think there's anything wrong with this part

.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
text-align: center;
}

/* Modal Content */
.modal-content {
    background-color: black;
    position: relative;
    margin: auto;
    padding: 50px 40px 50px 50px;
    display: inline-block;
}

/* The Close Button */
.close {
    color: #aaaaaa;
    float: right;
    position: relative;
    left: 20px;
    bottom: 35px;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #FFFFFF;
    text-decoration: none;
    cursor: pointer;
}
.modal_img{
        cursor: pointer;
}

As it currently is, I'm not getting any errors in the browser console, but the modal window is not popping up.

rpivovar
  • 3,150
  • 13
  • 41
  • 79

1 Answers1

0

Wrapping your javascript code with $document.ready(function(){...}) should solve the problem.

Hector Barbossa
  • 5,506
  • 13
  • 48
  • 70
  • Hmm, that didn't seem to do it. I've gotten this same modal window code working elsewhere, but putting it in this php page seems to have broken it. – rpivovar May 08 '17 at 00:02