Skip to content
Advertisement

How to pass the id of a client by a html button in javascript

here’s my problem:

I’m making a client system in javascript and php.

I would like to be able to delete the account of a customer who has a unique identifier (id=x) by clicking on a “Delete Customer” button.

<div class="menu-item px-5">
    <a id="kt_account_delete_account_btn" class="menu-link text-danger px-5"> Delete Customer</a>
</div>

The problem is that to bring a little dynamism, I treat the request in javascript in this way:

submitButton.addEventListener('click', function (e) {
        e.preventDefault();
            swal.fire({
                    text: "Êtes vous sûr de vouloir supprimer ce compte ?",
                    icon: "warning",
                    buttonsStyling: false,
                    showDenyButton: true,
                    confirmButtonText: "Oui",
                    denyButtonText: 'Non',
                    customClass: {
                        confirmButton: "btn btn-light-primary",
                        denyButton: "btn btn-danger"
                    }
                }).then((result) => {
                if (result.isConfirmed) {
                        $.post("details.php?id="+$("#admin_id_two").val()+"&client="+$("#client_idbis").val(), $("#delete_form").serialize()) // Code I usually use in a form
                        .done(function(data) {
                            if (data.erreur)
                            {
                                Swal.fire({
                                    text: data.erreur,
                                    icon: "error",
                                    buttonsStyling: false,
                                    confirmButtonText: "Ok, je recommence!",
                                    customClass: {
                                        confirmButton: "btn btn-primary"
                                    }
                                });
                            }
                            else if (data.link)
                            {
                                    Swal.fire({
                                        text: "Le compte utilisateur a correctement été supprimé !",
                                        icon: "success",
                                        buttonsStyling: false,
                                        confirmButtonText: "Ok, je continue!",
                                        customClass: {
                                            confirmButton: "btn btn-primary"
                                        }
                                    }).then(function (result) {
                                        if (result.isConfirmed) { 
                                            window.location.href = data.link;
                                        }
                                    });
                            }
                        })
                        .fail(function() {
                            Swal.fire({
                                text: "Une erreur est survenue lors de la requête. Veuillez ré-essayer.",
                                icon: "error",
                                buttonsStyling: false,
                                confirmButtonText: "Ok, je recommence!",
                                customClass: {
                                    confirmButton: "btn btn-primary"
                                }
                            });
                        }); 
                        
                        
                    } else if (result.isDenied) {
                    Swal.fire({
                            text: "Vous n'avez pas supprimer le compte de l'utilisateur.", 
                            icon: 'info',
                            confirmButtonText: "Ok",
                            buttonsStyling: false,
                            customClass: {
                                confirmButton: "btn btn-light-primary"
                            }
                        })
                }
            });
    });

My request is as follows:

I would like to recover the customer id ($clientinfo[‘id’] in php) and the administrator id ($login[‘id’] in php) in javascript to execute the request.

Could you help me?

Advertisement

Answer

add attribute data-id your a tag

like this:

<a id="kt_account_delete_account_btn" data-id="12" class="menu-link text-danger px-5"> Delete Customer</a>

remember: 12 is example, you should load id dynamically by php

and get in your Listener . like this:

submitButton.addEventListener('click', function (e) {
        e.preventDefault();
        let id = this.getAttribute('data-id');
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement