How to pass 2 variables through AJAX to another PHP

0

Well what I want to do is send 2 variables js with value of an input by AJAX to a file that receives them, for example: $ _POST ['idReceptor']. If you sent a single variable it is sent well but I can not send 2.

var idEmisor = $("#idEmisor").val();
           var idReceptor = $("#idReceptor").val();

            $.ajax({
                type: "POST",
                url: "/chat/conversacion.php",
                data: {"idReceptor="+idReceptor, "idEmisor="+idEmisor}
    
asked by Juampi 05.11.2018 в 21:27
source

2 answers

2

The property data of Ajax is a JSON object. In your case you have it badly written. Do this:

$.ajax({
    type: "POST",
    url: "/chat/conversacion.php",
    data: { idReceptor, idEmisor }
    // esto es lo mismo que { idReceptor: idReceptor, idEmisor: idEmisor }
    ...
});

This will be sent correctly.

    
answered by 05.11.2018 / 21:35
source
1

You can use Json data to send your parameters; as I show you next:

        $.ajax({
            type: 'post',
            url: '/chat/conversacion.php',
            data: {idReceptor: idReceptor, idEmsiro: idEmisor },
            contentType: "application/json; charset=utf-8",
            traditional: true,
            success: function (data) {
                ...
            }
        });
    
answered by 05.11.2018 в 21:34