Generate JSON from a form with equal names

Asked

Viewed 25 times

-1

I have the following list of inputs in a form:

inserir a descrição da imagem aqui

As you can see, each line has inputs with name static('idrequisica', 'idmaterial', etc)

<form method='POST' id='envia_dados' action='teste_post.php'>";

    foreach ($json as $key => $value) {
        $qtd_format = number_format($value->quantidade, 3, '.', '');

        echo "
            <tr>

                <td><input type=          'text'
                            name=         'idrequisicao'
                            class=        'idrequisicao'
                            readonly=     'readonly'
                            tabindex=     '-1'
                            value=        '$value->item'>
                </td>

                <td><input type=          'text'
                            name=         'idmaterial'
                            class=        'idmaterial'
                            readonly=     'readonly'
                            tabindex=     '-1'
                            value=        '$value->idmaterial'>
                </td>

                <td><input type=          'text'
                            name=         'descmaterial'
                            class=        'descmaterial'
                            readonly=     'readonly'
                            tabindex=     '-1'
                            value=        '$value->descmaterial'>
                </td>

                <td><input type=          'text'
                            name=         'quantidade'
                            class=        'quantidade'
                            id=           'quantidade-$n'
                            readonly=     'readonly'
                            tabindex=     '-1'
                            value=        '$qtd_format'>
                </td>

                <td><input type=          'number'
                            step=         '0.0001'
                            placeholder=  '0,0000'
                            name=         'valunit'
                            class=        'valores'                     
                            id_registro=  '$n'
                            tabindex=     '1'>
                </td>

                <td><input type=          'number'
                            step=         '0.01'
                            placeholder=  '0,00'
                            min=          '0'
                            max=          '100'
                            name=         'icms'
                            class=        'icms'
                            id=           'icms$n'
                            id_registro=  '$n'
                            tabindex=     '1'>
                </td>

                <td><input type=          'number'
                            step=         '0.01'
                            placeholder=  '0,00'
                            min=          '0'
                            max=          '100'
                            name=         'ipi'
                            class=        'ipi'
                            id=           'ipi$n'
                            id_registro=  '$n'
                            tabindex=     '1'>
                </td>

                <td><input type=          'text'
                            name=         'total'
                            class=        'total'
                            id=           'total-$n'
                            tabindex=     '-1'
                            readonly=     'readonly'>
                </td>

                <td>
                    <p class=             'abre_observacao'
                             id=          'idobservacao$n'
                             id_registro= '$n'
                             tabindex=     '-1'>
                    </p>
                </td>

            </tr>

            <tr>
                <td colspan='7'>
                    <textarea
                        name=             'observacao'
                        class=            'observacao'
                        id=               'text_observacao$n'
                        id_registro=      '$n'
                        tabindex=         '-1'
                        placeholder=      'Digite aqui a sua observação...'></textarea>
                </td>
            </tr>
        ";      
        $n += 1;
    }
    echo "</table>";

I would like to generate a JSON file, so it can be opened on this same screen but when I send all these parameters in the form, I can only follow the last line:

inserir a descrição da imagem aqui

Using the code segment to view this content:

<?php
$n = 0;
$m = 0;

foreach($_POST as $query_string_variable => $value) {
    if ($n == $m + 9) {
        echo "-------------------------------------<br/>";
        $m += 9;
    }

    echo "$query_string_variable  = $value <br/>";
    
    $n++;
}
?>

I tried to generate in this way:

$dados = $_POST;
$dados_json = json_encode($dados, JSON_UNESCAPED_UNICODE);
$path = "_js/_json/_requisicoes/";
$diretorio = dir($path);
$encontrado = 0;
while ($arquivo = $diretorio -> read()) {
    if ($idreq["id_req"] . "_r" . ".json" == $arquivo)  {
        $encontrado = 1;
    }
    //echo $arquivo;
}
if ($encontrado == 1) {
    echo "<br/><h1>O arquivo já existe!</h1><br/>l";
} else {
    $diretorio -> close();
    $fp = fopen("_js/_json/_requisicoes/" . $idreq["id_req"] . "_r" . ".json", "a");
    $jswrite = fwrite($fp, $dados_json);   
    fclose($fp);
}

But it keeps generating json with just the last line:

    {"idrequisicao":"026","idmaterial":"00.000.0057","descmaterial":"Material teste
 58","quantidade":"921.995","valunit":"8","icms":"8","ipi":"8","total":"","observacao":""}

I’m trying to get a json like this:

[{"item":"001","idmaterial":"00.000.0011","descmaterial":"Material teste
 17","quantidade":"1500"},
{"item":"002","idmaterial":"00.000.0042","descmaterial":"Material teste 
20","quantidade":"650.620"},
{"item":"003","idmaterial":"00.000.0039","descmaterial":"Material teste 
31","quantidade":"500.969"}]

If anyone can give some light. Thank you!

2 answers

-1


Transform the Names to the array format, the total for example would look like this: total[]. So you will have all the data in the post.

-1

Transforming what there is from name to array facilitates and so will have all the necessary information.

Browser other questions tagged

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