0

im new to all of this programming stuff and PHP. So every time i try to run this code the web page says:

Notice: Undefined index: tiempos in C:\wamp64\www\pruebaaaa\Act 1.php on line 14

I really don't know why. This is the code.Thanks in advance. (Sorry if my English is bad)

<!doctype HTML>
<HTML>
<head>
<title>Act 1</title>
<meta charset="utf-8">
</head>
<body>
 <form name="form1" method="POST" action="Act 1.php">
 Digite los segundos : <input name="tiempos" type="text" id="tiempos"> <br>
 <input type="submit" name="submit" value="Calcular">
 </form>
 <?php
 $segundos = $_POST['tiempos'];
 $horas = $segundos/3600;
 $minutos = $segundos/60;
 print 'Segundos : ' . $segundos;
 print '<br> Minutos : ' . intval($minutos);
 print '<br> Horas : ' . intval ($horas);
 ?>


</body>
</html>
Paul T.
  • 4,703
  • 11
  • 25
  • 29
Ark
  • 11

1 Answers1

0

The code to handle submit is executing before it has been submitted. The easiest way to stop that is to check if the POST is not null, like this

<!doctype HTML>
<HTML>
<head>
<title>Act 1</title>
<meta charset="utf-8">
</head>
<body>
 <form name="form1" method="POST" action="Act 1.php">
 Digite los segundos : <input name="tiempos" type="text" id="tiempos"> <br>
 <input type="submit" name="submit" value="Calcular">
 </form>
 <?php
if (isset ($_POST['submit']))  //<--- Checks if button has been pressed
{
 $segundos = $_POST['tiempos'] ?? 0;  //<--- Gives you 0 if the value is null
 $horas = $segundos/3600;
 $minutos = $segundos/60;
 print 'Segundos : ' . $segundos;
 print '<br> Minutos : ' . intval($minutos);
 print '<br> Horas : ' . intval ($horas);
}
 ?>
</body>
</html>
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41