Jump to content

Recommended Posts

before I had it like this but i kept getting undefined variable. The I used if (!isset ($_POST['loan']))  and it worked. 

 <?php
        $loan = $_POST['loan'];

        $interest = $_POST['interest'];
        $months = $_POST['months'];
        $amount = $_POST['amount'];
        
        function pmt($loan, $interest, $months) {
        
        $loan = $loans;
        $months = $months; 
$interest = $interest/12; 
        $amount = $loan * $interest/12 * pow((1 + $interest/12), $months) / (pow((1 + $interest/12), $months - 1));
        return $amount;
        }
?>

You asked the same question the other post, no need to make new ones.

If are going to ask a new question should post your current code.

 

http://php.net/manual/en/function.isset.php

 

You are running the code only if $_POST['loan'] is NOT set

 

Name your submit button (name="submit") in your form

 

Check if the form was submitted

Check if any post values are set and data you expect

Check if variables exist or not empty before trying to use them

In this case I will use is_numeric to check data types

Use curly braces

 

Look over the php manual and tutorial there, get familiar with everything.

Seems to me your teacher did not explain a lot or you took a nap.

<?php
function pmt($loan, $interest, $months){
    $amount = $loan * $interest / 12 * pow((1 + $interest / 12), $months) / (pow((1 + $interest / 12), $months - 1));
    return round($amount, 2);
}

//check if form submitted
if (isset($_POST['submit'])) {
    //check each if each post is set and it's value is data expect
    if (isset($_POST['loan']) && is_numeric($_POST['loan'])) {
        $loan = $_POST['loan'];
    }
    if (isset($_POST['interest']) && is_numeric($_POST['interest'])) {
        $interest = $_POST['interest'];
    }
    if (isset($_POST['months']) && is_numeric($_POST['months'])) {
        $months = $_POST['months'];
    }
}

//check if all 3 variables exist
if ($loan && $interest && $months) {
    //execute the code
    echo "Your monthly car payment will be " . pmt($loan, $interest, $months) . " a month, for " . $months . " months";
}

?>
  • Like 1

You had </form> in the wrong spot and also 2 names in form wrong as well

 

A working html and form using code i posted to you.

Compare and see the differences

<html>
<head>
<title>Homework 3</title>
 
</head>
<body>
<div>
        <p> Fill out this form to calculate monthly car payment</p>
        
        <form method="post" action="hw2test.php">
        
        <p>Loan Amount: <input type="text" name="loan" size="5" /></p>
        <p>Interest Rate: <input type="text" name="interest" size="5" /></p>
        <p>Number of Month: <input type="text" name="months" size="5" /></p>
        
        <input type="submit" name="submit" value="Calculate" />
		</form>
</div>
        
</body>
</html>

Your function to calculate is also wrong.

function pmt($loan, $interest, $months){
    $amount = $loan * ($interest / 12) * pow(1 + $interest / 12,$months) / (pow(1 + $interest / 12,$months) - 1);
    return round($amount, 2);
}
Edited by QuickOldCar
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.