Jump to content

what am i doing wrong with the php code? how do i code this equation to calculate monthly payment?


Recommended Posts

Im getting a parse error   Parse error: syntax error, unexpected ')' in /students/schow11/public_html/cs130a/hw2.php on line 13

 

How do i calculate this equation?

 

<?php 

        

        // Get the values from $_POST

        $loan = $_POST["loanamount"];

    $interest = $_POST["interest"];

    $months = $_POST["months"];

 

        // Calculate the monthly payment

        function pmt ($interest, $months, $loan) 

        {

        $months = $months;

        $interest = $interest / 12;

error--->$amount = $loan * $interest * pow((1 + $interest), $months,) / (1 - pow((1 + $interest), $months)); 

        }

        

        $amount = pmt ($interest, $loan, $months)

        echo "Your monthly carpayment will be;" . round($amount, 2 . " a month, for " . $months . "months";

 

    ?>  

 

html

 


 

<!DOCTYPE html>

<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"></form>

        

        <p>Loan Amount: <input type="text" name="loan" size="5" /></p>

        <p>Interest Rate: <input type="text" name="rate" size="5" /></p>

        <p>Number of Month: <input type="text" name="month" size="5" /></p>

        

        <input type="submit" name="submit" value="Caculate" />

    </div>

        

</body>

</html>

post-179680-0-52495000-1441484759_thumb.png

Edited by xaznbabyx

my teacher wants us to use the pow 
 

Assignment 2

For this assignment, you are going to write a form that will allow someone to calculate their monthly payment for a car loan. Consider the following:

  • The first page should contain a form with three text boxes, to allow the student to enter the following values:
    • Total amount of loan
    • Interest Rate
    • Number of months
  • The second page should calculate the monthly payment according to the following formula:

equation.gif

For example, for a loan amount of $18000, an interest rate of 4.9%, and 60 months, the payment should be $338.86

  • Your second PHP page will need to use the built-in function pow().
  • The Rate used in the formula above should be the interest rate expressed as a decimal (i.e., 0.049, not 4.9)
  • A person filling in the form can be expected to just enter numbers in the text boxes, no symbols (i.e., no $ or %). Do not worry about error checking for this.
  • Consider using the printf function or the round function to format your payment amount to two decimal places.

anyways thank you that fixed the parse error. Im getting another error for echo

 

Parse error: syntax error, unexpected 'echo' (T_ECHO) in /students/schow11/public_html/cs130a/hw2.php on line 17

Thank you so much. I found out what I was missing and im getting this error

 

Notice: Undefined index: loan in /students/schow11/public_html/cs130a/hw2.php on line 4 Notice: Undefined index: interest in /students/schow11/public_html/cs130a/hw2.php on line 5 Notice: Undefined index: months in /students/schow11/public_html/cs130a/hw2.php on line 6 Warning: Division by zero in /students/schow11/public_html/cs130a/hw2.php on line 13 Your monthly carpayment will be;0 a month, for months

 

<?php 

        
        // Get the values from $_POST
        $loan = $_POST["loan"];
        $interest = $P_POST["interest"];
        $months = $_POST["months"];
 
and
 
warning for this 
 
$amount = $loan * $interest * pow((1 + $interest), $months) / (1 - pow((1 + $interest), $months)); 

To become a programmer you have to have an eye for detail. Look carefully at your lines and you will see your error.

 

As for the div by zero - perhaps you should check the incoming parms to your functions to be sure that they are valid or simply check the values to be used in your calc before using them.

I don't get what am I suppose to do to get rid of the unidentified index? 

// Get the values from $_POST

        $loan = $_POST["loan"];

        $interest = $_POST["interest"];

        $months = $_POST["months"];

 

Notice: Undefined index: interest in /students/schow11/public_html/cs130a/hw2.php on line 13 Notice: Undefined index: months in /students/schow11/public_html/cs130a/hw2.php on line 14 Notice: Undefined variable: loan in /students/schow11/public_html/cs130a/hw2.php on line 24 Your monthly car payment will be;0 a month, for months

I dont' see these new lines in your previous code posting (which needs to be posted properly next time!). Have you changed things?

 

And as QOC says - names need to match. Quick lesson: The name attributes of input elements are what you need to look for in your $_POST array upon receiving the submit from the client. That's the connection.

It works now! Thank you, you guys!

 

Someone told me to place if (!isset ($_POST['loan']))  and it works.

 

But now my submit button isn't working. When I calculate the form it gives me this. 

 

http://hills.ccsf.edu/~schow11/cs130a/hw2.html

http://hills.ccsf.edu/~schow11/cs130a/hw2.php

 

Notice: Undefined variable: loan in /students/schow11/public_html/cs130a/hw2.php on line 28 Your monthly car payment will be;0 a month, for 60months

 

 

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Homework 2</title>
 
</head>
<body>
<pre>
        <p>Fill out this form to calculate monthly car payment</p>
        
        <form method="post" action="hw2.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>
        
         <p><input type="submit" value="Calculate"></p>
       
        </form>
 
  <?php
    
if (!isset ($_POST['loan'])) 
 
     
        // Get the values from $_POST
 
    $loan = 18000;
        $interest = 0.049;
        $months = 60;
        $amount = 338.86;
        
        function pmt($loan, $interest, $months) {
    
        $amount = $loan * $interest/12 * pow((1 + $interest/12), $months) / (pow((1 + $interest/12), $months - 1));
        return $amount;
        }
        
        $amount = pmt($loan, $interest, $months);
        print "Your monthly car payment will be;" . round($amount,2) . " a month, for " . $months . "months";
        
        ?> 
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.