Maintain values in text boxes

0

I'm starting in this PHP and I created this little form.

Whenever I give it when calculating clean the text boxes, it works OK, but I would like you not to clean the text boxes.

This is my HTML.

<form action="index.php" method="post">
<table>
<tr>
    <td>Evaluación Aplicativa</td>
    <td align="center"><input type="text" name="txtApli" id="txtApli" size="3"></td>
  </tr>
  <tr>
    <td>Examen Parcial</td>
    <td align="center"><input type="text" name="txtParcial" id="txtParcial" size="3"></td>
  </tr>
  <tr>
    <td>Examen Final</td>
    <td align="center"><input type="text" name="txtFinal" id="txtFinal" size="3"></td>
  </tr>

This is my PHP.

 <?php
  if (!empty($_POST)){

  $Apli = $_POST['txtApli'];
  $Parcial = $_POST['txtParcial'];
  $Final = $_POST['txtFinal'];

  if (empty($Apli)){
  echo "Ingrese el porcentaje de Evaluación Aplicatica<br>";
  $valida = false;
  }
  if (empty($Parcial)){
  echo "Ingrese el porcentaje de Examen Parcial<br>";
  $valida = false;
  }
  if (empty($Final)){
  echo "Ingrese el porcentaje de Examen Final<br>";
  $valida = false;
  }
  }
 ?>
    
asked by Angel Cayhualla 27.06.2018 в 23:50
source

1 answer

0

The way I can think of to get that is that the value of the inputs is an empty string or 0 (depends on what you want or need) if the form has not yet been sent or that field is empty, and if has sent and has value, as you keep it in a variable, you make the input have the value of that variable. Here is an example where I have also changed the way you send the error message.

HTML:

    <form action="index.php" method="post">
<table>
<tr>
    <td>Evaluación Aplicativa</td>
    <td align="center"><input type="text" name="txtApli" id="txtApli" size="3" value="<?php echo (!empty($Apli)) ? $Apli : ""; ?>"></td>
  </tr>
  <tr>
    <td>Examen Parcial</td>
    <td align="center"><input type="text" name="txtParcial" id="txtParcial" size="3" value="<?php echo (!empty($Parcial)) ? $Parcial : ""; ?>"></td>
  </tr>
  <tr>
    <td>Examen Final</td>
    <td align="center"><input type="text" name="txtFinal" id="txtFinal" size="3" value="<?php echo (!empty($Final)) ? $Final : ""; ?>"></td>
 </tr>
 </table>
<input type="submit" value="Enviar" /> 
  </form>
  <p><?php echo (!empty($mensaje)) ? $mensaje : ""; ?></p>

PHP:

    if (!empty($_POST)){

  $Apli = $_POST['txtApli'];
  $Parcial = $_POST['txtParcial'];
  $Final = $_POST['txtFinal'];
  $mensaje;

  if (empty($Apli)){
  $mensaje = "Ingrese el porcentaje de Evaluación Aplicatica<br>";
  $valida = false;
  }
  if (empty($Parcial)){
  $mensaje = "Ingrese el porcentaje de Examen Parcial<br>";
  $valida = false;
  }
  if (empty($Final)){
  $mensaje = "Ingrese el porcentaje de Examen Final<br>";
  $valida = false;
  }
  }

I've tried it just in case and it works perfectly.

    
answered by 28.06.2018 в 01:45