Problem sending input value (which is updated in a While) to another page via POST

0

I have a% re loop of% that creates fields according to the number of client counties. I need to pass the while variable via POST method to be inserted in the Database on another page.

In case you have only one municipality, POST works, but when you have more than one municipality, it continues to pass only one variable.

Follow the snippets of the code:

<?php    

   while($dado_ipl3 = mysqli_fetch_array($qry_ipl3)) { 

     $nome_municipio = $dado_ipl3['CIDADE'];
     $codmunicipio = $dado_ipl3['COD_CIDADE'];
?>

     <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">
  <div class="container">
       <div class="row">       
         <label class="col-md-3">IPL3 Distribuição do quantitativo total de acessos fisícos em serviço por tipo de usuário (mensal)</label>                       
       </div>


  <div class="row">
      <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
          <label class="col-md-3">Nome Município</label>
            <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
              <input type="text" name="nome_municipio" id="nome_municipio" class="form-control" value="<?php echo"$nome_municipio" ?>"   maxlength="18" size="18" title="Nome Município">
            </div>                       
      </div> 
  </div>


  <div class="row">
    <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
      <label class="col-md-3">Codigo Municipio</label>
        <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
          <input type="text" name="codmunicipio[]" id="codmunicipio" class="form-control numero_livre" value="<?php echo $codmunicipio; ?>"  maxlength="7" size="7">
       </div>                       
     </div> 
  </div>


 <div class="row">
     <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
        <label class="col-md-3">Acesso físico P.F.</label>
          <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
            <input type="text" name="IPL3_valor_F" id="IPL3_valor_F" class="form-control numero" value="0"   maxlength="18" size="18" title="Quantitativo de Acesso físico em serviço pelo tipo da Pessoa Física">
          </div>                       
      </div> 
  </div>

  <div class="row">
      <div class="col-md-6 col-sm-6 col-xs-12 form-group has-feedback">        
        <label class="col-md-3">Acesso físico P.J.</label>
          <div class="col-md-9 col-sm-6 col-xs-6 form-group has-feedback">
            <input type="text" name="IPL3_valor_J" id="IPL3_valor_J" class="form-control numero" value="0"  maxlength="18" size="18" title="Quantitativo de Acesso físico em serviço pelo tipo da Pessoa Jurídica">
          </div>                       
      </div> 
  </div>


  </div>
</div>

<?php   


   }


?>

page you receive:

    $codmunicipio = $_POST['codmunicipio'];
    $valor_ipl3_f = $_POST['IPL3_valor_F']; 
    $valor_ipl3_j = $_POST['IPL3_valor_J'];  


   $sql3 = "INSERT INTO financeiro_sici_anatel_ipl3 (ano, mes, 
    fistel,codmunicipio, IPL3_valor_F, IPL3_valor_J)

    VALUES ('$ano_upload', '$mes_uplooad', '$fistel','$codmunicipio', 
     '$valor_ipl3_f', '$valor_ipl3_j')"; 


      $resultado3 = $banco->pesquisarBD($sql3);

In case I want to pass all these updated fields:

$codmunicipio', '$valor_ipl3_f', '$valor_ipl3_j
    
asked by anonymous 13.12.2017 / 14:26

2 answers

1

Try to use:

<input type="text" name="nome_municipio[]" />

And on the page where you will receive $ _ POST make another repeat loop to get the values

foreach ($_POST['nome_municipio'] as $pos => $valor) {
    $nome_municipo = $valor;
}
    
13.12.2017 / 14:33
0
  • When there is more than one county, there will obviously be more than one county code, so since your form contains brackets [ ] in name of the input Codigo Municipio , which means that it is sent as vector or array to the receiver, you must use foreach () to move through this array.
  • If your form is correct, the input values IPL3_valor_F and IPL3_valor_J are always set to 0 , so there is no need to retrieve these values on the insert page. Just put in VALUES of statement insert value 0
  • Another detail is that there is no hint that Nome Município is part of the insert statement so it's also unnecessary to retrieve this value.
  • In fact, if I not are mistaken about the above in items 2 and 3, the page you receive should look like this:

    if (isset($_POST['codmunicipio'])){
    
        $codmunicipio = $_POST['codmunicipio'];
        //desnecessários
        //$valor_ipl3_f = $_POST['IPL3_valor_F']; 
        //$valor_ipl3_j = $_POST['IPL3_valor_J'];
        //A cada iteração, a variável "$value" automaticamente recebe o valor de cada item do array,
        foreach ($codmunicipio as $value) {
             $sql3 = "INSERT INTO financeiro_sici_anatel_ipl3 (ano, mes, 
                fistel,codmunicipio, IPL3_valor_F, IPL3_valor_J)
                VALUES ('$ano_upload', '$mes_uplooad', '$fistel','$value', 
                 0, 0)"; 
        }
    
    }
    

    If you pass all these $codmunicipio', '$valor_ipl3_f', '$valor_ipl3_j fields updated:

    if (isset($_POST['codmunicipio'])){
    
        $codmunicipio = $_POST['codmunicipio'];
        $valor_ipl3_f = $_POST['IPL3_valor_F']; 
        $valor_ipl3_j = $_POST['IPL3_valor_J'];
    
        for($i = 0; $i<count($codmunicipio); $i++) {
    
           $sql3 = "INSERT INTO financeiro_sici_anatel_ipl3 (ano, mes, 
                fistel,codmunicipio, IPL3_valor_F, IPL3_valor_J)
                VALUES ('$ano_upload', '$mes_uplooad', '$fistel','$codmunicipio[$i]', 
                 '$valor_ipl3_f[$i]', '$valor_ipl3_j[$i]')"; 
        }
    
    }
    
      

    where inputs IPL3_valor_F and IPL3_valor_J must contain brackets at the end of their name

        
    13.12.2017 / 21:06