Kelsey
Kelsey

Reputation: 921

How to Make a JavaScript Alert For Each MySQL Row After a Div is Clicked?

When a user clicks the div with the id "showhidestaff", I would like to create a PHP while loop to get all the MYSQL rows' ids from the table "spc_calendar_calendars", then alert each id one after the other. So far, using the code I have posted below, the JavaScript only alerts the first ID (from the MYSQL table). Here is the code I have so far:

<script>
    var button22 = document.getElementById('showhidestaff');
    button22.onclick = function() {
if($( "#showhidestaff" ).text() == 'Hide All Calendars') {



    <?php 
    $selectcals = "SELECT * FROM spc_calendar_calendars";
    $results = mysqli_query($dbc, $selectcals);

    while($row = mysqli_fetch_array( $results ))
            {
                $calendar_id = $row['id'];
                $calendar_status = $row['status'];

                    ?>
                    alert("<?php echo $calendar_id;?>");

                    <?php               
            }

    ?> 
    $("#showhidestaff").text('Show All Calendars');     
     }
 </script>

Thank you for any help. All help is greatly appreciated.

Upvotes: 0

Views: 357

Answers (1)

Qaisar Satti
Qaisar Satti

Reputation: 2762

now you will alert all ids and status too

<script>
        var button22 = document.getElementById('showhidestaff');
        button22.onclick = function() {
    if($( "#showhidestaff" ).text() == 'Hide All Calendars') {



        <?php 
        $selectcals = "SELECT * FROM spc_calendar_calendars";
        $results = mysqli_query($dbc, $selectcals);

        while($row = mysqli_fetch_array( $results ))
                {
                    $calendar_id[] = $row['id'];
                    $calendar_status[] = $row['status'];

                        ?>


                        <?php               
                }

        ?> 
    alert("<?php echo implode(',',$calendar_id);?>");
    alert("<?php echo implode(',',$calendar_status);?>");
        $("#showhidestaff").text('Show All Calendars');     
         }
     </script>

Upvotes: 1

Related Questions