AJAX button action in foreach

I have loop foreach, where i write data and buttons. This code work only from one button, first in loop and the rest does not work.

JS:

jQuery(document).ready(function($) {

jQuery(‘#przyciskUlubione’).click(function() {

var data = {
‘action’: ‘UlubioneDodaj’,
‘id_plik’: $(“#przyciskUlubione”).val()

};

jQuery.post(ajaxurl, data, function(response) {

if(response == “1”){

$(“#przyciskUlubione”).css(‘background-color’,’#4hui7d’);

alert(“wybor 1”);

}

if(response == “2”){

$(“#przyciskUlubione”).css(‘background-color’,’#f47121′);

alert(“wybor 2”);

}

});

});

});

PHP:

public function execute(){

add_action( ‘wp_ajax_nopriv_UlubioneDodaj’, array( $this, ‘UlubioneNiezalogowany’ ));
add_action( ‘wp_ajax_UlubioneDodaj’, array( $this, ‘UlubioneZalogowany’ ));

}

public function UlubioneNiezalogowany(){

// $id_plik = $_POST[‘id_plik’];

echo ‘1’;

wp_die();

}

public function UlubioneZalogowany(){

// $id_plik = $_POST[‘id_plik’];

echo ‘2’;

wp_die();

}

………….

some_function(){

foreach(){

echo ‘<button id=”przyciskUlubione” value=”‘.$plik[‘id’].'”> Ulubione </button>’;


}

}

Read more here:: AJAX button action in foreach

Leave a Reply

Your email address will not be published. Required fields are marked *