json array with javascript serialize not working

Asked

Viewed 113 times

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 picking him up 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 the serialize it does not send correctly to PHP by giving a print_r() in PHP it returns:

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

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

    [nome] => 
)

I needed it to take all the form data correctly. The problem is only one that is an array and contains the property numero, operadora e contato, i.e., the telephone.

1 answer

1


Hello,

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

instead of

telefone[0].numero etc..

trial telefone[0]['numero']

Browser other questions tagged

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