a calculator in php

Asked

Viewed 432 times

1

I need a PHP code from a calculator, which adds 4 notes and divides by 4 and if the final average is greater than or equal to 6 that it shows approved, if not disapproved. 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>
  • 1

    You could post the HTML code of your form here?

  • can yes, more aq in the comments does not fit

  • I’ll edit the question and post there may be??

  • Elaborate on your question Cristina. Stackoverflow doesn’t work like this: "I want code X for yesterday because I have a deadline." Which part of the code did you not understand? What is your question exactly? Where is the corresponding HTML code?

  • can’t you do the calculation? I think it’s best to change the names of inputs, take out that cipher.

  • It is pq in the code in PHP, it should show the result of the sums qnd do the division and if it was greater or equal to 6 it was to give approved and if it was smaller it was to fail, only goes to the condition of Else, regardless of the value that gives. and I don’t find the error

  • HTML code put in question

  • rray, input numbers are the variables I used in PHP

  • If it is falling only on Else its variables must be as zero or null look: $nt1 = $_GET[$nt1]; so you say for php, want the Intel $_GET only he doesn’t exist ...

  • More want the user to give the value of the variable understand? what can I put in place? Can help me?

  • In all variable php starts with $ just take it out of the name of inputs and $nt1 = $_GET[$nt1];, let alone $nt1 = $_GET['nt1'];

  • Got it, it was missing simple quotes in the variable. Obg rray

Show 7 more comments

2 answers

2


You are picking up your form data in a wrong way.

Instead of $_GET[$nt1]; should be $_GET['nt1']; and so in others.

See the working code here.

Follow the full 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";
    } 
?>
  • It was the lack of even simple quotes. Muuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuo Obliaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  • 1

    Yeah, use the name in his html without the $. And similarly call him with the quotes and without the $ inside $_GET[];

2

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

He understands the $nt4 as a variable of PHP, since to declare them you use $, even if you put between "" not right, and only replace by "nt4", does not forget to change in html too.

Browser other questions tagged

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