xaznbabyx Posted September 7, 2015 Share Posted September 7, 2015 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.ed...cs130a/hw2.html http://hills.ccsf.ed.../cs130a/hw2.php Quote Link to comment Share on other sites More sharing options...
xaznbabyx Posted September 7, 2015 Author Share Posted September 7, 2015 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; } ?> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 7, 2015 Share Posted September 7, 2015 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. https://secure.php.net/manual/en/getting-started.php <?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"; } ?> 1 Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 7, 2015 Share Posted September 7, 2015 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> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 7, 2015 Share Posted September 7, 2015 (edited) 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 September 7, 2015 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.