简体   繁体   中英

php loop value pass to javascript

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.

你可以尝试echo荷兰国际集团的变量,像这样一个新的脚本:

<?php echo "<script type='text/javascript'>var myVar = value; </script>"; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM