Skip to content
Advertisement

Send data that has nothing to do with button to modal using AJAX

I have this Ajax function:

        $(document).ready(function() {
            $('.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() 
                    },
                    success: function(data) {
                        $('#employee_detail2').html(data);
                        $('#adddata2').modal("show");
                    }
                });

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

It takes this button properties(value and id) and sends them to the modal via post:

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

Now, I need to take a data that has nothing to do with the button ($row[‘nomeDepartamento’];):

<td><?php echo $row['nomeDepartamento']; ?></td>

and send it via the same ajax function to the modal.

How would I do this?

Thank you!

Advertisement

Answer

This is how your button will look like

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

and retrive the $row['nomeDepartamento']; & after click funciton declare variable

var nomeDepartamento = $(this).attr("data-field-name");

and send this in data:{} and after success change html of any element like $("element").html(nomeDepartamento) and there you go.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement