how to recover the data sent by post in php?

0

I have a form and when I send it, it connects to a php file where it inserts the data, then an alert appears that says correct insertion, and returns to the form, all right up there, the bad thing is that when return to the form the fields will be deleted as I can do so that you do not erase the form data, thanks

html code of the form

     <div id="formulario_esquema">

  <form action="" method="POST">
             <legend>Primera fase: <?php echo $apellidop.' '.$apellidom.' '.$nombre.'/'.$nro_caso.'/'.$tipo_tratamiento;?></legend>

<?php 

  $resultado_tipotuberculosis = mysqli_query($conexion,"SELECT * FROM localizacion_enfermedad");
  $resultado_localizacion = mysqli_query($conexion,"SELECT * FROM especificacion_localizacion");
  $resultado_comorbilidad = mysqli_query($conexion,"SELECT * FROM comorbilidad")
 ?>

  <div class="campo_formularios">
             <input type="hidden"  name ="dni" id="dni" value=<?php echo $dni?>>
             <input type="hidden" name="nro_caso" id="nro_caso" value=<?php echo $nro_caso?>>
               <input type="hidden" name="nombre" id="nombre" value=<?php echo $nombre?>>
             <input type="hidden" name="apellidom" id="apellidom" value=<?php echo $apellidom?>>
             <input type="hidden" name="apellidop" id="apellidop" value=<?php echo $apellidop?>>
             <input type="hidden" name="tipo_tratamiento" id="tipo_tratamiento" value=<?php echo $tipo_tratamiento?>>

              <select class="tipo_tuberculosis" id="tipo_tuberculosis" name="tipo_tuberculosis">
               <option value="0" disabled selected>Tipo de Tuberculosis</option>
                  <?php  
                   while ($rowtitu = $resultado_tipotuberculosis -> fetch_array()) 
                   {

                   ?>

          <option value="<?php echo $rowtitu['cod_localizacion_enf']; ?>"><?php echo $rowtitu['nombre_localizacion'];  ?></option>
          <?php
               }

            ?>

              </select>




              <select class="localizacion" id="localizacion" name="localizacion">
              <option value="0" disabled selected>Localizacion</option>
              <?php
                       while($roweslo = $resultado_localizacion-> fetch_array())
                       {
                     ?>
                        <option value="<?php echo $roweslo['cod_esp_loc']; ?>"><?php echo $roweslo['nombre_especificacion_loc'];  ?></option>

                        <?php 
                             }
                         ?>

              </select>

              <select class="comorbilidad" id="comobilidad"  name="comobilidad" onchange="mostrartabla()">
               <option value="0" disabled selected >Comorbilidad</option>
                   <?php
                       while($rowcom = $resultado_comorbilidad -> fetch_array())
                       {
                     ?>

                     <option value="<?php echo $rowcom['cod_comobilidad']; ?>"><?php echo $rowcom['nombre_comorbilidad'];  ?></option>

                     <?php
                          }
                       ?>



              </select>

              <div class="fecha_ini">
                   <label>Feche inicio</label><input id="infechaini" type="date" name="infechaini" onblur="obtenerfechafinf1();" style="height:30px;">  
              </div>

             <div id="fecha_fin">
             </div>

   </div> 

          <div id="tabla_tratamiento">

          </div>    


  <div class='botones'>
          <div class="btnenviarf2">
             <input type='submit' id='btnenviar' class='btnenviar' name='btnenviar' value='GUARDAR FASE 1' onclick="this.form.action='guardarf1.php'; this.form.submit();">
          </div>
          <div class="pasarf2">    
             <input type='submit' id='pasar' class='pasar' name='pasar' value='PASAR A FASE 2' onclick="this.form.action='esquemaf2.php'; this.form.submit();">
          </div>
    </div>

  </form> 
</div>

php code

 <?php
     include '../conexionbd.php';

    date_default_timezone_set('America/Bogota');


    $dni = $_POST["dni"];
    $cod_loc_enf=$_POST["tipo_tuberculosis"];
    $cod_comorbilidad=$_POST["comobilidad"];
    $cod_esp_loc=$_POST["localizacion"];
    $nro_caso=$_POST["nro_caso"];
    $cod_fase ='1';
    $flag_fase1='1';
    $flag_fase2='0';
    $fecha_inicio=$_POST["infechaini"];
    $fecha_fin=$_POST["fechafin"];


    $consulta_codtramiento = "SELECT cod_tratamiento FROM tratamiento where cod_loc_enf='$cod_loc_enf' and cod_esp_loc='$cod_esp_loc' and cod_comorbilidad='$cod_comorbilidad'";
    $consultar_pacf1 ="SELECT flag_ver_fase1 FROM det_pac_tra where flag_ver_fase1='1' AND dni='$dni'";

    $resultado_pacf1 = mysqli_query($conexion,$consultar_pacf1) or die(mysqli_error($conexion));

    if(mysqli_num_rows($resultado_pacf1)>0){
        echo'
          <script>
        alert("EL PACIENTE YA SE ENCUENTRA REGISTRADO EN PRIMERA FASE");
        window.history.go(-1);
          </script>
        ';
        exit;
    }
    //echo $consulta_codtramiento;

    $resultado_codtratamiento = mysqli_query($conexion,$consulta_codtramiento) or die(mysqli_error($conexion));
    while($fila = $resultado_codtratamiento->fetch_array()) 
    {

        $cod_tratamiento=$fila["cod_tratamiento"];

    }

    //echo $cod_tratamiento;




    $insertar_dettratamiento ="INSERT INTO det_pac_tra(dni,nro_caso,cod_tratamiento,cod_fase,flag_ver_fase1,flag_ver_fase2,fecha_inicio_fase1,fecha_fin_fase1) values('$dni','$nro_caso','$cod_tratamiento','$cod_fase','$flag_fase1','$flag_fase2','$fecha_inicio','$fecha_fin')";



    $resultado = mysqli_query($conexion,$insertar_dettratamiento) or die(mysqli_error($conexion));

    if($resultado){

        //secho $insertar_dettratamiento;

        echo "<script>
         alert('LA FASE 1 SE ENCUETRA REGISTRADA CORRECTAMENTE');
       window.history.go(-1);

         </script>
        ";


    }else{
    echo "<script>
     alert('Ocurio un error lo sentimos');
     window.history.go(-1);
     document.getElementById('tipo_tuberculosis').value=0;
     document.getElementById('comobilidad').value=0;
     document.getElementById('localizacion').value=0;

     </script>
    ";
}

mysqli_close($conexion);




  ?>
    
asked by ingswsm 19.06.2017 в 18:34
source

1 answer

2

In PHP you have three arrays to collect the data you send:

  • $ _ GET for parameters that go in the same URL
  • $ _ POST for the parameters sent by post (as your case)
  • and $ _REQUEST which is a mixture of the previous ones.

They are "superglobal", so they are available in any field.

To keep the value that was sent you must put the "option" that corresponds to "selected", not all of them, example for the first "select":

<option value="<?php echo $rowtitu['cod_localizacion_enf'] . ($cod_loc_enf == $rowtitu['cod_localizacion_enf'] ? '" selected="selected' : ''); ?>">

And remove the "selected" from the first, if none is selected, the first one is selected by default.

    
answered by 19.06.2017 в 19:19