json array with serialize javascript not working

1

I have this form:

<form>
        <select name="data">
            <option>0831</option>
            <option selected>4731</option>
            <option>9831</option>
        </select>
        <br />
        <input type="text" name="telefone[0].numero" placeholder="Telefone"> <br />
        <input type="text" name="telefone[0].operadora" placeholder="Telefone"> <br />
        <input type="text" name="telefone[0].contato" placeholder="Telefone"> <br />
        <input type="text" name="telefone[1].numero" placeholder="Telefone"> <br />
        <input type="text" name="telefone[1].operadora" placeholder="Telefone"> <br />
        <input type="text" name="telefone[1].contato" placeholder="Telefone"> <br />
        <input type="text" name="endereco[]" placeholder="Endereço"> <br />
        <input type="text" name="endereco[]" placeholder="Endereço"> <br />
        <input type="text" name="endereco[]" placeholder="Endereço"> <br />
        <input type="text" name="nome" placeholder="Nome">
    </form>

I'm catching it via serialize :

$(document).ready(function(){

        var form = $('form').serialize();

        $.ajax({
            url: 'index_server.php',
            type: 'POST',
            data: form,

            success: function(callback){

                console.log(callback);
            }
        });
    })

The problem is that with serialize it does not send correctly to PHP, giving a print_r() in PHP it returns:

Array
(
    [data] => 4731
    [telefone] => Array
        (
            [0] => 
            [1] => 
        )

    [endereco] => Array
        (
            [0] => 
            [1] => 
            [2] => 
        )

    [nome] => 
)

He needed to get all the data from the form correctly. The problem is only the one that is an array and contains the numero, operadora e contato property, that is, the phone .

    
asked by anonymous 05.02.2017 / 16:39

1 answer

1

Hello,

I think the problem is in the names of the inputs of the form.

instead of

telefone[0].numero etc..

try telefone[0]['numero']

    
05.02.2017 / 16:59