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

Asked

Viewed 44 times

-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 wanted to know how I can send this array with all the data inside to ajaxDoEnvioMensagem.php and be able to receive them in php;

My goal is to get this data , receive in the.php sending and right after calling the php method of the objects.php; AQUI EU QUERO RECEBER O VETOR JAVASCRIPT NO PHP

1 answer

0

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

<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 want. Type:

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

I created a new vector and Success function only to test. A php code similar to this one is able to read the sent data:

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

foreach($ids as $indice => $chave){
  echo $indice . ' => ' . $chave . '<br>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.