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

Asked

Viewed 137 times

0

I have a looping loop while which creates fields according to the number of customer municipalities. I need to pass the variable codmunicipio via method POST to be inserted into the database on another page.

In the case if there is only one municipality the POST works but when there is more than one municipality it continues passing only one variable.

Follow the code snippets:

<?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

2 answers

1

Try to use:

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

And on the page where you will receive the $_POST Make another repeat loop to pick up the values

foreach ($_POST['nome_municipio'] as $pos => $valor) {
    $nome_municipo = $valor;
}
  • Keep taking only one value.

  • You have to rename all inputs as array nome_municipio[], when you get to the other page, use a print_r($_POST) for you to see the values.

  • i am trying to pass like this: foreach ([$_POST['codmunicipio'], $_POST['Ipl3_valor_f'], $_POST['Ipl3_valor_j']] as list ($value1, $value2, $value3)) { $codmunicipio = $value1;, $valor_ipl3_f = $value2;, $valor_ipl3_j = $value3;

  • Because I have to pass ipl3_valor_f and ipl3_valor_j to EACH municipality

0

  1. When there is more than one municipality, evidently there will be more than one municipality code, therefore, since your form contains square brackets [ ] in the name of input Codigo Municipio, meaning that it is sent in the form of vector or array to the receiver, it is necessary to make use of foreach () to traverse this array.
  2. If your form is correct, the input values IPL3_valor_F and IPL3_valor_J are always set with value 0, this way there is no need to redeem these values on the Insert page. Just put in the VALUES of the declaration insert the value 0
  3. Another detail is that there is no indication that Nome Município is part of the declaration insert therefore unnecessary also recover this value.

Otherwise, if I nay is wrong 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)"; 
    }

}

In case you pass all these fields $codmunicipio', '$valor_ipl3_f', '$valor_ipl3_j up-to-date:

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]')"; 
    }

}

and in the inputs IPL3_valor_F and IPL3_valor_J shall contain brackets at the end of their respective name

Browser other questions tagged

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