Home > Back-end >  How to pass the id of a client by a html button in javascript
How to pass the id of a client by a html button in javascript

Time:07-18

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 >
    <a id="kt_account_delete_account_btn" > 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?

CodePudding user response:

add attribute data-id your a tag

like this:

<a id="kt_account_delete_account_btn" data-id="12" > 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');

CodePudding user response:

Assuming variable $userID contains the ID of the user, you can append user id to data-id attribute using php as shown then use Javascript to get the data-id attribute as pointed out by Ali SSN. See using Jquery in snippet

<a id="kt_account_delete_account_btn" data-id="<?php echo $userID;?>" > Delete Customer</a>

$(document).on('click','.menu-link',function(e){
    e.preventDefault();
    var userID=$(this).attr('data-id');
    alert(userID);
})
a{
color:blue;
text-decoration:underline;
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div >
    <a id="kt_account_delete_account_btn" data-id="5"> Delete Customer</a>
</div>

  • Related