How to send a Javascript vector to php and display them?

-1

I can create my javascript array and store the values inside an array called ArmazenaIds

ArmazenaIds = new Array();
    $('.checkboxs').each(function () {
        var estadoDoCheck = $(this).prop('checked');
        if(estadoDoCheck == true) {


            id = $(this).attr('iddoemail');
            ArmazenaIds.push(id);
            alert(ArmazenaIds);

        }

    });
    console.log(ArmazenaIds);
    $.ajax({
        url:'ajaxDoEnvioMensagem.php',
        type:'post',
        data:ArmazenaIds,
        beforeSend:function() {
            alert('carregando');

I would like to know how I can send this array with all the data inside to the ajaxDoFeedMessage.php and to be able to receive them in the php;

My goal is to get this data, get it in the messaging.php and soon after calling the php method of the objects.php;

    
asked by anonymous 19.11.2017 / 01:29

1 answer

0

A small change to the data field is missing as a parameter for the $.ajax function:

<script>
    var ArmazenaIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    $.ajax({
    url:'ajaxDoEnvioMensagem.php',
    type:'post',
    data: {ids: ArmazenaIds},
    beforeSend:function() {
            alert('carregando');
    },
    success: function(dados){
        document.write(dados);
    }

    });
</script>

In the date field you can pass an object with as many attributes as you wish. Type:

data: {campo1: [elemento1, elemento2], campo2: string}

I created a new vector and the success function is only for testing. A php code similar to this is able to read the data sent:

<?php
$ids = $_POST['ids'];

foreach($ids as $indice => $chave){
  echo $indice . ' => ' . $chave . '<br>';
}
    
19.11.2017 / 02:15