Skip to content

Instantly share code, notes, and snippets.

@0xMatt
Forked from anonymous/mortgageJavascript.html
Created January 31, 2016 23:24
Show Gist options
  • Save 0xMatt/6a9f3fe5640c235c5496 to your computer and use it in GitHub Desktop.
Save 0xMatt/6a9f3fe5640c235c5496 to your computer and use it in GitHub Desktop.
theomega76PHP-HELL
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
</head>
<body>
<?php
/**
* Created by PhpStorm.
* User: Jimmy
* Date: 1/16/2016
* Time: 7:04 PM
*/
if(!empty($_POST["principalInput"]) && !empty($_POST["interestInput"]) && !empty($_POST["paymentInput"])){
$principal = $_POST["principalInput"];
$interestRate = $_POST["interestInput"];
$monthlyPayment = $_POST["paymentInput"];
$tempMonthlyInterest = ($principal * (($interestRate / 12) / 100));
$tempNewPrincipal = $principal - $monthlyPayment + $tempMonthlyInterest;
if($tempNewPrincipal > $principal){
echo "<h1>ERROR! Program Execution Stopped.</h1><br>";
echo "The values used for monthly payment and/or interest rate will cause a data overflow.";
exit();
}
//echo "<table border=\"1\">";
if(!empty($principal) && !empty($interestRate) && !empty($monthlyPayment)){
/*echo "Principal: $".number_format($principal, 2)."<br>";
echo "Interest: ".number_format($interestRate, 2)."% <br>";
echo "Monthly Payment: $".number_format($monthlyPayment, 2)."<br><br>";
echo "<tr>";
echo "<th>Payment #</th>";
echo "<th>Principal</th>";
echo "<th>Interest</th>";
echo "<th>Payment</th>";
echo "<th>Balance</th>";
echo "</tr>";*/
class mortgageInfo{
public $startPrin = 0;
public $interest = 0;
public $payment = 0;
public $endPrin = 0;
}
$mortgageInfoVar = new mortgageInfo();
$mortgageAraWrapper = array();
$mortgageAra = array();
$paymentNum = 0;
$newPrincipal = 0;
while($principal > 0){
$monthlyInterest = ($principal * (($interestRate / 12) / 100));
//$mortgageInfoVar->interest = $monthlyInterest;
//$mortgageInfoVar->payment = $monthlyPayment;
//$mortgageInfoVar->startPrin = $principal;
$newPrincipal = $principal - $monthlyPayment + $monthlyInterest;
$paymentNum++;
if($newPrincipal < 0){
$monthlyPayment = $principal + $monthlyInterest;
//$mortgageInfoVar->payment = $monthlyPayment;
$balance = 0;
//$mortgageInfoVar->endPrin = $balance;
}
else{
$balance = ($principal + $monthlyInterest - $monthlyPayment);
}
// echo "<tr>";
// echo "<td>$paymentNum</td>";
// echo "<td>".number_format($principal, 2)."</td>";
// echo "<td>".number_format($monthlyInterest, 2)."</td>";
// echo "<td>".number_format($monthlyPayment, 2)."</td>";
// echo "<td>".number_format($balance, 2)."</td>";
// echo "</tr>";
//$mortgageInfoVar->endPrin = $balance;
if($paymentNum <= 1){
$principal = $principal;
}
else{
$principal = $balance;
}
/*this is the MORE elegant means of producing the output*/
//array_push($mortgageAra, $mortgageInfoVar);
//echo json_encode($mortgageInfoVar);
// /*this is the caveman way to produce the output, not to mention over-utilize resources i.e. two arrays*/
// $mortgageAra['startPrin'] = $mortgageInfoVar->startPrin;
// $mortgageAra['interest'] = $mortgageInfoVar->interest;
// $mortgageAra['payment'] = $mortgageInfoVar->payment;
// $mortgageAra['endPrin'] = $mortgageInfoVar->endPrin;
$mortgageAra['startPrin'] = $principal;
$mortgageAra['interest'] = $monthlyInterest;
$mortgageAra['payment'] = $monthlyPayment;
$mortgageAra['endPrin'] = $balance;
$mortgageAraWrapper[] = $mortgageAra;
}
header('Content-Type: application/json');
echo json_encode($mortgageAraWrapper);
exit;
}
else{
echo "<b>Error! All fields must contain valid data to display output.</b>";
}
}
else{
echo "<b>Error! All fields must contain valid data to display output.</b>";
}
?>
<script>
var temp = JSON.parse(<?php($mortgageAra); ?>);
alert(temp);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Theomega76| Mortgage Table with Inputs</title>
<meta charset="utf-8" />
<!-- Move to bottom later on for better performance -->
<script src="js/jquery-1.11.2.js"></script>
</head>
<body>
<form action="mortgage_Javascript.php" method="post" onsubmit="displayTable()">
<fieldset>
<legend>Mortgage Information</legend>
Principal: <input type="text" name="principalInput"> <br>
Annual Interest Rate: <input type="text" name="interestInput"> <br>
Monthly Payment: <input type="text" name="paymentInput"> <br>
<input id="displayTable" type="submit" value="Submit">
</fieldset>
</form>
<table border="1">
<tr>
<th>Payment #</th>
<th>Principal</th>
<th>Interest</th>
<th>Payment</th>
<th>Balance</th>
</tr>
</table>
<script>
// var table = document.getElementById("displayTable");
// var test = document.getElementById("test");
//
// table.addEventListener('Submit', displayTable(),false);
//
// function displayTable(){
//
// var request = new XMLHttpRequest();
// var url = "mortgage_Javascript.php";
//
// request.open("POST", url, true);
// request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//
//
// request.onreadystatechange == function(){
//
// if(request.readyState == 4 && request.status == 200){
// showText(request.responseText);
// }
// }
// request.send();
//
// function showText(response){
//
// var mortInfo = JSON.parse(response);
// var sPrin = mortInfo.startPrin;
// myText.textContent = "S " + sPrin;
// }
//
// }
function displayTable() {
var tbl = document.createElement("TABLE");
tbl.setAttribute("id", "mortTable");
document.body.appendChild(tbl);
var head1 = document.createElement("TH");
head1.setAttribute("id", "mortH1");
document.getElementById("mortTable").appendChild(head1);
var headLbl1 = document.createTextNode("Payment #");
head1.appendChild(headLbl1);
var head2 = document.createElement("TH");
head2.setAttribute("id", "mortH2");
document.getElementById("mortTable").appendChild(head2);
var headLbl2 = document.createTextNode("Principal");
head2.appendChild(headLbl2);
var head3= document.createElement("TH");
head3.setAttribute("id", "mortH3");
document.getElementById("mortTable").appendChild(head3);
var headLbl3 = document.createTextNode("Interest");
head3.appendChild(headLbl3);
var head4 = document.createElement("TH");
head4.setAttribute("id", "mortH4");
document.getElementById("mortTable").appendChild(head4);
var headLbl4 = document.createTextNode("Payment");
head4.appendChild(headLbl4);
var head5 = document.createElement("TH");
head5.setAttribute("id", "mortH4");
document.getElementById("mortTable").appendChild(head5);
var headLbl5 = document.createTextNode("Balance");
head5.appendChild(headLbl5);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment