0

I am making a loan calculator and I want to run some calculations that are being posted from a form using the POST method. The code is not executing when I run it. Where am I am missing it?

I have run the code without the function and it seems to be working well, but the moment I put it in a function its not running.

function loancal()
{
  if(isset($_POST['submit'])) {
    $principal = $_POST['principal'];
    $intrstRate = $_POST['intrest'];
    $tenure = $_POST['tenure'];
    $result = ($principal * $intrstRate * $tenure) ;
    echo $result;
  } else {
    echo '00.00';
  }
}

This is the line that is calling the function after it has been submitted:

<h1 class="title is-2"> $<?php loancal(); ?></h1>

I am expecting the out to change from $00.00 to for example a calculated result, but the output is still the same $00.00

This is the form (excerpt).

<form method="post" action="">
  <input type="text" name="principal">
  <input type="text" name="intrest">
  <input type="date">
  <input type="text" name="tenure">
  <button type="submit">Calculate</button>
  <button type="reset" >Clear</button>
</form>
Styx
  • 9,863
  • 8
  • 43
  • 53
Raymond
  • 13
  • 7

2 Answers2

2

So my very first answer is your issue, you need to give your submit button a name.

<button name="submit" class="button is-rounded is-primary" type="submit">Calculate</button>
imvain2
  • 15,480
  • 1
  • 16
  • 21
0

Here's one way to get the code to execute:

<?php

function loancalc() {
$result = '0.00';
if( isset( $_POST['submit'] )){
// inspecting submitted values so that... 
    foreach ($_POST as $key => $value){
      $bool[$key] = ctype_print( $value );
    }
// ... if proven valid, then make assignments               
    if ( $bool['principal'] && $bool['interest'] && $bool['tenure'] ) {
// array destructuring available since PHP 7.1
      [$principal, $interestRate,$tenure] = 
      [$_POST['principal'],
      $_POST['interest'],
      $_POST['tenure']];
      $result = $principal * $interestRate * $tenure;
     } // inner if
 } // if POSTed
    echo number_format( $result,2,'.',',' ); // default English representation
} // end func
?>

  <html>
  <head>
    <title>Untitled</title>
<style>
#submit {
  background: #000;
  color: lime;
}

#clear {
  background: #000;
  color: cyan;
}

input {
  background: #ffffee;
  color: #303;
}

h1 {
  font-size: 32pt;
  margin-bottom: 3em;
  color: #f0c;
}
 </style>
  </head>

  <body>
    <h1>$&nbsp;
      <?php loancal(); ?>
    </h1>

    <form method="post" action="">
      <input type="text" name="principal" id="principal">
      <input type="text" name="intrest" id="intrest">
      <input type="date">
      <input type="text" name="tenure" id="principal">
      <button type="submit" name="submit" id="submit">Calculate</button>
      <button type="reset" id="clear">Clear</button>
    </form>
  </body>
  </html>

The form values will only be properly submitted if they possess name attributes. Id attributes are optional and handy for front-end code manipulation.

Important point: Be sure to treat user submitted data as suspect, i.e. possibly tainted. In this example, a simple check makes sure that the the user input values only contain printable characters.

(Related demo of PHP code here.)

Refinements:

  • gave $result default value and eliminated an if-else clause
  • formatted the number with number_format() to make the number more user-friendly.
  • added some styling for fun :)
  • slevy1
    • 3,797
    • 2
    • 27
    • 33