0

I need to take this button value:

<button type="button" class="btn btn-outline-primary view_data2" id=" <?php echo $row['idFuncionarios']; ?>" value = "<?php echo $row['dia']; ?>" name = "envio">Ver</button></td>

And send it ($row['dia']) to the backend via POST method.

I already have this ajax function, which takes the button id and sends it to the same url I want using POST!

            $('.view_data2').click(function() {
                var employee_id2 = $(this).attr("id");

                $.ajax({
                    url: "includes/accomUser.inc.php",
                    method: "post",
                    data: {
                        employee_id2: employee_id2
                    },
                    success: function(data) {
                        $('#employee_detail2').html(data);
                        $('#adddata2').modal("show");
                    }
                });

                $('#addmodal2').modal("show");
            });
        });

How can I do that?

Thank you!

1 Answer 1

1

You can send the value of the button to the backend via POST by using this code

$('.view_data2').click(function() {
    var employee_id2 = $(this).attr("id");

    $.ajax({
        url: "includes/accomUser.inc.php",
        method: "post",
        data: {
            employee_id2: employee_id2,
            button_value: $(this).val() // $(this).val() will return value attribute
        },
        success: function(data) {
            $('#employee_detail2').html(data);
            $('#adddata2').modal("show");
        }
    });

    $('#addmodal2').modal("show");
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, James. It worked! Sometimes I just can't think in this easy things! If i wanted to send, like, values that are shown in my front end: like <td><?php echo $row['nomeDepartamento']; ?> </td> that has nothing to do with the button, how would i send it via the same ajax? Thank you for the help!
You should use a <form> to surround the codes, then you can use <input type="hidden" name="{your data name} value="{your value}"/> then change the button type to submit, and use jQuery to handle the on submit function, you can use $(this).serializeArray() to get all value inside the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.