0

I am working on a assignment in PHP but cannot get my results to show up now on a separate page, or at all. After adding in my IF statements, everything stopped working but I am not getting an error message. Any help would be appreciated. I have supplied all of my code below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Loans
</title>
<link rel="stylesheet" type="text/css" href="simple.css">
<?php include('header.html'); ?>
</head>
<body>
<form action="loans1.php" method="post">
    <table>
        <tr>
            <td><Label id="principal" name="principal">Enter current balance.</Label></td>
            <td><input id="P" name="P" type="textbox"></input></td>
        </tr>
        <tr>
            <td><Label id="interest" name="interest">Enter current interest rate.</Label></td>
            <td><input id="I" name="I" type="textbox"></input></td>
        </tr>
        <tr>
            <td><Label id="duration" name="duration">Enter the duration of the loan.</Label></td>
            <td><input id="L" name="L" type="textbox"></input></td>
        </tr>
    </table>
<input type="submit" value="Calculate" /> <br />
<br /><label id="result" name="result" text=""></label>
</form>
<?php
    
    @ $p=$_POST['P'];
    @ $i=$_POST['I'];
    @ $l=$_POST['L'];
    @ $result=$_POST['R'];
    @ $vFlag=true;
    @ $calculate=$_POST['Calculate'];
    include('footer.html');
?>    
</body>
</html>

That is for my main page. The second code is for my PHP code.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
<link rel="stylesheet" type="text/css" href="simple.css">
</head>
<body>
<form action="loans.php" method="post">
<?php
   include('header.html');
  # Constants that will be used

  define(MINP, 1000);               #  Minumum principle
  define(MAXP, 1000000);            #  Maximum principle
  define(MININT, 1);                #  Minimum interest rate
  define(MAXINT, 100);              #  Maximum interest rate
  define(MINL, 1);                  #  Minium Length
  define(MAXL, 100);                #  Maximum Length

  # User input
    
    $p = $_POST['P'];
    $i = $_POST['I'];
    $l = $_POST['L'];
    $result = $_POST['R'];
    $vFlag  = true;
    $calculate=$_POST['Calculate'];

    if (isset($_POST['Calculate']))
    {  # Begins if (isset($_POST['submit']))
        if (($p < MINP) || ($p > MAXP))
        {
            echo 'The principale must be between 1,000 and 1,000,000. ' ;
            $vFlag = false;
        }

        if (($i < MININT) || ($i > MAXINT))
        {
            echo 'The interest rate must be between 1% and 100%.';
            $vFlag = false;
        }

        if (($l < MINL) || ($l > MAXL))
        {
            echo 'The length of the loan must be between 1 and 100  years.';
            $vFlag = false;
        }

        if ($vFlag)
        {
            $mi = ($i / 1200);
            $lresult = ($l * 12);
            $a = (1 + $mi);
            $result = (($p * $mi * (pow($a, $lresult))) / ((pow($a, $lresult)) - 1));

            echo "The principal is " . $p . "<br />";
            echo "The interest is " . $i . "<br />";
            echo "The duration/length is " . $l . "<br />";
            echo "The monthly interest is " . $mi . "<br />";
            echo "The number of payments is " . $lresult . "<br />";
            echo "The monthly payment is " . round($result, 2) . "<br />";
        }

        else
        {
            echo "Error in input<br />";
        }
    } # Ends if (isset($_POST['submit']))
  include('footer.html');
?>
</body>
</html>

Any help at all would be greatly appreciated.

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Rated X
0

your submit button has name="submit", so you should be using that in $_POST, NOT 'Calculate'. You must use the NAME of the field:

WRONG:

$calculate=$_POST['Calculate'];
 if (isset($_POST['Calculate']))

CORRECT:

/* after this, $calculate should be set whatever the VALUE attribute is set to. In this case Calculate */
 $calculate=$_POST['submit'];
 if (isset($_POST['submit']))
0

It still is not displaying the results. I made those changes, and it still takes me to a blank page.

0

on your form, this:

<input type="submit" value="Calculate" />

needs a NAME attribute:

<input type="submit" name="Submit" value="Calculate" />

Notice the UPPER CASE "S" for Submit (lowercase "submit" is 'reserved' in javascript). So be sure to use "Submit" in your php code as well:

$calculate=$_POST['Submit'];
 if (isset($_POST['Submit']))
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.