Submit Multidimensional Array

Asked

Viewed 33 times

2

I have a form with input checkbox and text

'<input type="checkbox" name="formandos_servicos[]" value="'+data['id']+'">'+
'<input type="text" name="nome_funcionario_servicos[]" value="'+data['title']+'" >'+
'<input type="text" name="naturalidade_servicos[]" value="'+data['naturalidade']+'" placeholder="Naturalidade">'+

I count the input you load into Form

for($i = 0; $i < $count; $i++) {
$data[] = array(
'formandos_servicos' => $formandos_servicos[$i],
'nome_funcionario_servicos' => $nome_funcionario_servicos[$i]
);
}

At print_r($data) the result is:

[1] => Array
(
[formandos_servicos] => 680
[nome_funcionario_servicos] => Alberto Damião Pimenta
)

[2] => Array
(
[formandos_servicos] => 678
[nome_funcionario_servicos] => Celestino José Faria Oliveira
)

The result of [traines_services] corresponds to the 2nd and 3rd checkbox selected. The result of [function_services_name] corresponds to the 1st and 2nd input text.

Does anyone know how to match the checkbox with input text ?

  • See if you include: $data[$i], make sure to return

  • 1

    Thanks André. Gives the same result... The result of the selection of input checkbox is correct, but the result of input text is not. :(

  • I don’t know how to match checkbox with Texts input...

1 answer

0

From the checkbox group named "formandos_servicos[]" only those marked (checked) will be sent. The other elements are sent all, but the correspondence at the time of receiving will be according to the indexing of the received checkbox. A foreach is more suitable:

foreach ($formandos_servicos as $index => $value) {
        $data[] = array(
            'formandos_servicos' => $value,
            'nome_funcionario_servicos' => $nome_funcionario_servicos[$index]
        );
}

Browser other questions tagged

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