Web lists-archives.com

Re: [PHP] Calculator beginner issue




Op 8/05/2016 om 20:22 schreef Erwin Noever:
Hello guys,

I am sorry to disturb you.

I followed a tutorial and i am a newbe to php but want to learn.

My calculator doesn't work, i dont see what is wrong.
Please can someone explain what the issue is or give me a hint.
Thank you in advance.
Html Code:

<form method="post" action="calc.php">
     <input type="text" name="nummer1" placeholder="nummer1" />
     <label for="kies">kies</label>
     <select name="reken" id="kies">
         <option>selecteer</option>
         <option value ="plus">+</option>
         <option value ="min">-</option>
         <option value ="delen">÷</option>
         <option value ="maal">x</option>
     </select>
     <input type="number" name="nummer2" placeholder="nummer2" />
     <input type="submit" value="calculeer" />

</form>

php code:

function bereken ()
{
     $nummer1 = $_POST['nummer1'];
     $nummer2 = $_POST['nummer2'];

     switch ($_POST['reken'])
     {
         case "plus":
             $plus = $nummer1 + $nummer2;
             return $plus;
         break;

         case "min":
             $min = $nummer1 - $nummer2;
             return $min;
             break;

         case "delen":
             $deel = $nummer1 / $nummer2;
             return $deel;
             break;

         case "maal":
             $maal = $nummer1 * $nummer2;
             return $maal;
             break;
         default:
             echo "bereken op een andere maniet";
     }
}

     echo "het resultaat is :". bereken();

?>
<br>
<a href="index.html">terug</a>

You try to use $_POST variables directly in your function. You should pass variables as arguments of your function:
function bereken ($nummer1, $nummer2)
Then call your function: bereken($_POST['nummer1'], $_POST['nummer2'])

Besides of that, you have to check first if your form has been sent before calling your function. You also should do some kind of validation of the entered values to prevent unexpected results.

Kind regards,
Steven


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php