php - How in jquery to get the form generated by FormHelper::postLink() with option 'block' => true in Cakephp4?

one text

Solution:

I just found a solution.

I add the name of the form associated to the postLink in the 'data-form-name' attribute of the link and then I use this attribute to get the form :

$('.item-box .delete')
    .each(function(index){
        var formName = $(this).attr("onclick").match(/post_[A-Za-z0-9]+/);
        console.log(formName);
        $(this).attr('data-form-name', formName); // Add the name of the form associated to the postLink() in data-form-name attribute
    })
    .removeAttr('onclick')
    .click(function(e){
        e.preventDefault();
        var form = $('form[name="' + $(this).data('form-name') + '"]'); // get the form depending on data-form-name attribute
        var url = $(form).attr("action");
        if($(this).data('confirm-message'))
            message_confirmation = $(this).data('confirm-message');
        else
            message_confirmation = 'Confirm ?';

        if(confirm(message_confirmation)) {

            parent = $(this).parents('.item-box');

            $.ajax({
                type: 'POST',
                cache: false,
                url: url,
                data: $(form).serialize()
            })
            .done(function(response) {
                                parent.slideUp(
                                    'fast',
                                    function(){ // fonction de callback quand le fadeOut est fini 
                                        parent.remove(); // remove enlève l'élément du DOM (fadeout le masque!) 
            
                                    }
                                );
                            })
            .fail(function(error) {
                                alert("Delete error (" + error.statusText + ")");
                                location.reload();
                            });
        }
            
        return false;
    });

Source