Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Problem AJAX i PHP "While"
diamondking
post 20.02.2018, 00:21:03
Post #1





Grupa: Zarejestrowani
Postów: 100
Pomógł: 0
Dołączył: 7.02.2014

Ostrzeżenie: (0%)
-----


Witajcie mam kod PHP "Powiadomienień" :

  1. if(mysql_num_rows($wynik) > 0) {
  2.  
  3. while($r = mysql_fetch_assoc($wynik)) {
  4.  
  5.  
  6. <table class="table noti">
  7.  
  8. <tr>
  9. <input type="hidden" id="idnoti" value="'.$r['id'].'">
  10. <td><span style="background:'.$r['color'].'; padding-top: 5px;" class="noft-icon fa '.$r['icon'].'"></span></td>
  11. <td class="textnoti">'.$r['tresc'].'</td>
  12. <td class="closenoti"><i class="fa fa-times-circle" aria-hidden="true"></i></td>
  13. </tr>
  14.  
  15.  
  16. </table>
  17.  
  18. ';
  19.  
  20. }
  21. }


oraz AJAX:

  1. <script type="text/javascript">
  2. //Rejestracja
  3.  
  4.  
  5. $(document).ready(function(){
  6.  
  7. $(".closenoti").click(function () {
  8.  
  9. var idnoti = $("#idnoti").val();
  10.  
  11. $.ajax({
  12. url:"noti/change.php",
  13. method:"POST",
  14. data:{idnoti:idnoti},
  15. dataType:"JSON",
  16. success:function(data)
  17. {
  18.  
  19. }
  20. })
  21. });
  22.  
  23. });
  24.  
  25. </script>


Problem polega na tym, że załóżmy iż wyświetla się tak:

1. ID: 7
2. ID: 6
3. ID: 1

mimo, że klikam na "closenoti" przy id 1 to zmienia mi status id 7

Pomoże ktoś biggrin.gif ?
Go to the top of the page
+Quote Post
aras785
post 20.02.2018, 00:39:31
Post #2





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


Cześć,

dzieje się tak ponieważ pobierasz zawsze ten sam element (var idnoti = $("#idnoti").val();). Używaj $(this) po kliknięciu. Na upartego wystarczy zmienić u Ciebie jedną linijkę:

var idnoti = $("#idnoti").val();
na: var idnoti = $(this).parent().find("#idnoti").val();

ale ja bym napisał to mniej więcej tak:

  1. if(mysql_num_rows($wynik) > 0) {
  2. while($r = mysql_fetch_assoc($wynik)) {
  3. echo '
  4. <table class="table noti">
  5. <tr>
  6. <td><span style="background:'.$r['color'].'; padding-top: 5px;" class="noft-icon fa '.$r['icon'].'"></span></td>
  7. <td class="textnoti">'.$r['tresc'].'</td>
  8. <td class="closenoti"><a href="#" data-action="change" data-id="'.$r['id'].'"><i class="fa fa-times-circle" aria-hidden="true"></i></a></td>
  9. </tr>
  10. </table>
  11. ';
  12. }
  13. }

  1. <script type="text/javascript">
  2. //Rejestracja
  3. $(document).ready(function() {
  4. $('table.noti').on('click','[data-action="change"]', function(e) {
  5. e.preventDefault();
  6. var id = $(this).attr('data-id');
  7. $.ajax({
  8. url:"noti/change.php",
  9. method:"POST",
  10. data:{idnoti:id},
  11. dataType:"JSON",
  12. success:function(data) {
  13. console.log(data);
  14. }
  15. })
  16. });
  17. });


Ten post edytował aras785 20.02.2018, 00:42:53
Go to the top of the page
+Quote Post
diamondking
post 20.02.2018, 09:15:01
Post #3





Grupa: Zarejestrowani
Postów: 100
Pomógł: 0
Dołączył: 7.02.2014

Ostrzeżenie: (0%)
-----


Dziękuję!

Można zamknąć.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.03.2024 - 05:50