php loop value pass to javascript
Get the solution ↓↓↓i'm using php to fetch data from sql and i'm using javascript for the popup modal of each data fetched from sql.
i want to pass the value inside while loop in php to a var in javascript that reflect what was clicked in mysql_fetch_array.
here's my code:
php
i made the first data a button(hoping this will trigger the popup modal)
the names of the ['id'] are 'fac01' and 'fac02'.
function viewfac()
{
$query = "SELECT * FROM faculty";
$result = mysql_query($query);
echo "<table class='col-sm-6'>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td><button id='". $row['id'] ."'>" . $row['id'] . "</button></td><td>" . $row['lastname'] . ", " . $row['firstname'] . " " . $row['middlename'] . "</td></tr>";
echo "</form>";
}
echo "</table>";
echo "<script>
</script>";
}
javascript
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById('fac01');
// 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';
}
}
THIS IS MY PROBLEM. IT'S WORKING LIKE THAT, BUT AS I SAID ABOVE I HAVE 2 DATA = fac01 and fac02. but it's only working when i click fac01 coz that's what it says. but i want to change the id depending on the php above. i wanted to do this getElementById('fac01') to getElementById('< ? php ? >') but i cant get the value of $row['id'] outside the loop and pass here.
// Get the button that opens the modal
var btn = document.getElementById('fac01');
pls help me. thanks.
Answer
Solution:
You could tryecho
ing the variable to a new script like so:
<?php echo "<script type='text/javascript'>var myVar = value; </script>"; ?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: php_network_getaddresses: getaddrinfo failed: temporary failure in name resolution
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.