a calculator in php [closed]

1

I need a PHP code from a calculator, which adds 4 notes and divides by 4 and if the final media is greater than or equal to 6 that it shows approved, otherwise fail. SCRR

<?php

       $nt1 = $_GET[$nt1];
       $nt2 = $_GET[$nt2];
       $nt3 = $_GET[$nt3];
       $nt4 = $_GET[$nt4];
       $mf = ( $nt1 + $nt2 + $nt3 + $nt4)/4;
       echo " $nt1, $nt2, $nt3, $nt4,  $mf ";

      if ( $mf >= 6  ) {
       echo "aprovado" ;

      }
      else{
       echo "Reprovado";

        } 
    ?>

HTML code

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">

  <title> calculadora </title>
 </head>
  <body>
  <form action="calculadora.php" method = "get">
  <table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
    <tbody>
        <tr>
            <td>Primeiro bimestre</td>
            <td><input name="$nt1" type="text" /></td>
        </tr>
        <tr>
            <td>Segundo bimestre</td>
            <td><input name="$nt2" type="text" /></td>
        </tr>
        <tr>
            <td>terceiro bimestre</td>
            <td><input name="$nt3" type="text" /></td>
        </tr><tr>
            <td>quarto bimestre</td>
            <td><input name="$nt4" type="text" /></td>
        </tr>
        <tr>
            <td></td>
            <td><input name="bt_validar" type="submit" value="Calcular" /></td>
        </tr>
    </tbody>
</table>


  </body> 
</html>
    
asked by anonymous 05.10.2016 / 17:29

2 answers

2

You're getting the data from your form in the wrong way.

Instead of $_GET[$nt1]; it should be $_GET['nt1']; and so on the others.

See the code working here .

Here is the complete example code based on your code:

HTML:

<!-- não esqueça de abrir com tag <html>, inserir o <head> e fechar ele e tmabém de abrir <body> -->

<form id="formulario" name="formulario" method="get" action="calcular.php">
Nota 1 <input id="nt1" name="nt1" type="text" /><br />
Nota 2 <input id="nt2" name="nt2" type="text" /><br />
Nota 3 <input id="nt3" name="nt3" type="text" /><br />
Nota 4 <input id="nt4" name="nt4" type="text" /><br />
<input id="btnenviar" name="btnenviar" type="submit" value="Calcular" />
</form>

<!-- feche o <body> e feche o <html> -->

PHP file calculate.php:

<?php

    $nt1 = $_GET['nt1'];
    $nt2 = $_GET['nt2'];
    $nt3 = $_GET['nt3'];
    $nt4 = $_GET['nt4'];
    $mf = ( $nt1 + $nt2 + $nt3 + $nt4)/4;
    //echo " $nt1, $nt2, $nt3, $nt4,  $mf "; Esta linha vai imprimir as notas e e também a média

    if ( $mf >= 6  ) {
        echo "aprovado" ;
    }
    else{
        echo "Reprovado";
    } 
?>
    
05.10.2016 / 17:43
2

When you put $nt4 = $_GET[$nt4];

It understands $nt4 as a variable of PHP , since to declare them you use $ , even if you put between "" not right, and only replace with "nt4" , no Forget to change in html as well.

    
05.10.2016 / 18:07