0

I obviously have a technical problem that I just cannot figure out. First of all, being called a “newbie” is being very kind to me! Here’s a quick overview of my problem: I have a MYSQL database which contains data on a number of home audio files. I’ve managed to get data back to a results page when searching based on content some of the table fields – title, description, tags...

| Title | Description |Tags | PLAY AUDIO|

| Title 1| Desc 1 |Tags | BUTTON |

| Title 2| Desc 2 |Tags | BUTTON |

I have also modified a simple Modal window code that I found online to create a kind of popup Audio Player that will show info about the file and allow it to be played. My aim is to have the user click on a specific button under Play Audio and it will open the Modal with the appropriate audio file ready to be played. However, it does not work. It will open the first one but not any of the following ones. I do know that the id must be unique and so I tried this

<div id="audioModal<?php echo $row['audio_id']; ?>" >
<button id="audioBtn<?php echo $row['audio_id']; ?>" >Play Audio</button>

I'm guessing the main problem is when I try to use this in the javascript code as I cannot use the php id in it.

<script type="text/javascript">
var modal = document.getElementById("audioModal");
// Get the button that opens the modal
var btn = document.getElementById("audioBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
  modal.style.display = "block";
}
// 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>

But no luck. All suggestions are more than welcome. Thanks in advance.

Here is the associated CSS if it helps.

<style> 
.modal{
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  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 */
}


/* Modal Header */
.modal-header {
  padding: 2px 10px;
  background-color: #98a9b5;
  color: black;
}

h2{
    color: white;
    text-decoration: underline;
}

/* Modal Body */
.modal-body {
    padding: 2px 10px;
    /*margin: auto 20%; */
    background-color: #98a9b5;
    /*border: solid;*/
    }

/* Modal Footer */
.modal-footer {
  padding: 2px 10px;
  background-color: #3e6077;
  color: white;
}


/* Modal Content */
.modal-content {
  background-color: #3e6077;
  margin: 10% auto; /* 15% from the top and centered */
  padding: 10px;
  border: 1px solid #888;
  width: 30%; /* Could be more or less, depending on screen size */
}


/* The Close Button */
.close {
  color: white;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
} 
</style>
thecraw
  • 1
  • 1
  • 1
    Does this answer your question? [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) – Robo Robok Oct 13 '21 at 21:19
  • 1
    *"I cannot use the php id in it"* - Why can't you? What is stopping you from strategically placing a `` where you want it in that code just like you would anywhere else on the page? What happens when you try? – David Oct 13 '21 at 21:30
  • Thank you @Robo Robok. It might well be the answer but I'm not really sure which option would be the easiest to implement and how to proceed... – thecraw Oct 17 '21 at 19:21
  • Hi David. The problem arises when I try to use within the javascript for the modal and btn variables. I tried this: var modal = document.getElementById("audioModal"); but unfortunately it did not work as that would have solved my problem. – thecraw Oct 17 '21 at 19:32
  • @thecraw do you have one modal elements or multiple? There are many ways to deal with what you're trying to do, you can consider putting audio ID in the `data-audio-id` attribute (for example) on the button. Then you can read it on click with `event.currentTarget.dataset.audioId` and open the modal using this value. – Robo Robok Oct 17 '21 at 20:21
  • Hi @Robo Robok. I have added the css used on my page if that helps. It looks like I still have so much to learn even although I'm 60... My site is purely for the benefit of immediate family and as such, I'm just looking for a quick and simple way to get this working. Thank you for your input. – thecraw Oct 20 '21 at 19:30
  • @thecraw hello again, it's certainly never too late to learn something new :) I think I'd consider putting more data to JavaScript and make your template more JavaScript-driven, because it seems like every modal is the same, except using different audio file (please correct me if I'm wrong). You could get all your database results in, let's say `$rows` variable and then put it as `` - this is a popular approach to make your backend data available for the frontend. And then you can use `data` in your other JavaScript code. – Robo Robok Oct 20 '21 at 20:15
  • Thank you so much @Robo Robok for your time and patience. I will see if I can get my head around this and finally get a working solution. – thecraw Nov 02 '21 at 20:09
  • Sorry @Robo Robok. Forgot to answer your actual question. Yes, all modals are the same but I want each to use a different audio file. – thecraw Nov 02 '21 at 20:27

1 Answers1

0

getElementById('audioModal') returns you only the element with exact 'audioModal' id. While you created bunch of unique ids.

Javascript getElementById based on a partial string - probably this one will help you, but you need to take the list of your buttons and assign the same clickhander to all of them. Not the best approach to be honest.

  • Thank you for an answer @Roman. It appears that I have backed myself into a corner with this one. Any suggestions on the best (and simplest) way to proceed would be really appreciated. – thecraw Oct 17 '21 at 19:36