PHP/AJAX Why is my getAttribute only targeting the first line in the foreach code

Aron Van Es

So what I'm trying to do here is to show a list of users. In this list, each user has a button that sends two Ids to my database which is different for every user in the array. I send this data with an ajax call which you can see below.

The problem is, that the code only works for the first user in the list and not the others. My guess is that it is not working properly because every list item has the same id name so it only targets the first item with the .getelementbyid selector.

So my question is if there is any other way to make the buttons send the ids for each user individually.

I have tried using the .sibling selector but I'm not really sure how to implement it correctly.

<div class="content">
<div class="container-fluid">
    </div>
    <div class="row">
    <ul>
        <?php foreach ($module as $row) :?>
            <div class="col-md-8">
                <div class="toggleHolder">
                    <span class="toggler"><span>▾</span>Show More</span>
                    <span class="toggler" style="display:none;"><span>▴</span> Show Less</span>
                </div>
                <li><p id="post" data-id="<?php echo $row['id'] ?>"><?php echo $row['naam'] ?></p></li>

                <div class="showpanel" style="display: none;">
                    <li><p><?php echo $row['beschrijving'] ?></p></li>

                                <?php foreach ($patient as $row2): ?>
                                <div class="col-md-10">
                                        <div class='lists'>
                                            <li class="flex-item">
                                                <p class="text-left border-bottom post2" id="post2" data-id="<?php echo $row2['id'] ?>"><?php echo $row2['voornaam'].' '.$row2['achternaam'];?></p>
                                                <input id="btnSubmit" type="submit" value="Module toewijzen"/>
                                            </li>

                                    </div>
                                </div>
                                <?php endforeach; ?>
                </div>

            </div>
        <?php endforeach; ?>

    </ul>
    </div>
</div>

$(document).ready(function () {
    $("#btnSubmit").on("click", function (e) {
        console.log("clicked");

        // tekst vak uitlezen
        var module_id = document.getElementById("post").getAttribute("data-id");
        var user_id = document.getElementById("post2").getAttribute("data-id");
        // via AJAX update naar databank sturen
        $.ajax({
            method: "POST",
            url: "AJAX/clientmodule.php",
            data: {user_id: user_id,module_id: module_id} //update: is de naam en update is de waarde (value)

        })

            .done(function (response) {

                // code + message
                if (response.code == 200) {

                }
            });

        e.preventDefault();
    });
});
Aron Van Es

Thanks that definitely helped me in the right direction. with your code, all the buttons started working but I still had the problem that it only posted the ids from the first line.

this is how I eventually solved it:

$(function() {
    $(".btnSubmit").on("click", function(e) {
        e.preventDefault();
        console.log("clicked");

        var $container = $(this).closest('.col-md-10');
        var module_id = $container.closest('.col-md-8').find(".post").data('id');
        var user_id = $container.find(".post2").data('id');

        console.log("module : "+module_id);
        console.log("user : "+user_id);

         $.ajax({
            method: "POST",
            url: "AJAX/clientmodule.php",
            data: {
                user_id: user_id,
                module_id: module_id
                },
        success: function() {
    // callback logic here...
          }
      });
   });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

only the first line of my javascript code works

From Dev

Why is my code not reading the first line?

From Dev

Why does my converted html string only show the first line?

From Dev

why setTimeout() only run my code once,at first time?

From Dev

Why is the compiler only compiling the first two sections of my code?

From Dev

Why is there only one line as result for my js code?

From Dev

why my TCP server code send a SYN/ACK on only first packet or only on the first connection?

From Dev

Why is my script is only getting the first row of my php table? below is my code

From Dev

Why Is My C# Code Only Displaying The First Row from my Database?

From Dev

python only recognizes first line of my input

From Dev

Why is only the first line of the condition executed?

From Dev

Why is this program only printing out the first line?

From Dev

Why is the first line of my file not being counted?

From Dev

Why my listview only detects the first checkbox?

From Dev

Why is only my first HTTP request running?

From Dev

Why is my for only working on the first variable?

From Dev

Why is my for loop only grabbing first element?

From Dev

My bufferedread only reading the first line of my file?

From Dev

currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

From Dev

Why doesn't my code read this line?

From Dev

Why is my program printing to only one line instead of a seperate line?

From Dev

Why is only part of my code not rendering in view

From Dev

Why is my list function only outputting first value of my array?

From Dev

Why is my variable data getting dropped after the first foreach loop?

From Dev

Why is my variable data getting dropped after the first foreach loop?

From Dev

Why is my script only working for the last line of my text file?

From Dev

My code is working and the first line is displaying correctly but the second line is concatenated with the first line etc

From Dev

Why database first classes differ from my code first classes

From Dev

PHP Why does my foreach loop only give 1 result?

Related Related

  1. 1

    only the first line of my javascript code works

  2. 2

    Why is my code not reading the first line?

  3. 3

    Why does my converted html string only show the first line?

  4. 4

    why setTimeout() only run my code once,at first time?

  5. 5

    Why is the compiler only compiling the first two sections of my code?

  6. 6

    Why is there only one line as result for my js code?

  7. 7

    why my TCP server code send a SYN/ACK on only first packet or only on the first connection?

  8. 8

    Why is my script is only getting the first row of my php table? below is my code

  9. 9

    Why Is My C# Code Only Displaying The First Row from my Database?

  10. 10

    python only recognizes first line of my input

  11. 11

    Why is only the first line of the condition executed?

  12. 12

    Why is this program only printing out the first line?

  13. 13

    Why is the first line of my file not being counted?

  14. 14

    Why my listview only detects the first checkbox?

  15. 15

    Why is only my first HTTP request running?

  16. 16

    Why is my for only working on the first variable?

  17. 17

    Why is my for loop only grabbing first element?

  18. 18

    My bufferedread only reading the first line of my file?

  19. 19

    currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

  20. 20

    Why doesn't my code read this line?

  21. 21

    Why is my program printing to only one line instead of a seperate line?

  22. 22

    Why is only part of my code not rendering in view

  23. 23

    Why is my list function only outputting first value of my array?

  24. 24

    Why is my variable data getting dropped after the first foreach loop?

  25. 25

    Why is my variable data getting dropped after the first foreach loop?

  26. 26

    Why is my script only working for the last line of my text file?

  27. 27

    My code is working and the first line is displaying correctly but the second line is concatenated with the first line etc

  28. 28

    Why database first classes differ from my code first classes

  29. 29

    PHP Why does my foreach loop only give 1 result?

HotTag

Archive