html in PHP
Hi! I'm having problems with the php file i'm working on.
I'm supposed to make an html code work on php.
Here's the html code:
Code:
<html>
<script type="text/JavaScript">
function calcATable()
{
aLoaned=document.getElementById("aLoaned").value;
i=document.getElementById("iLoan").value/100;
n=document.getElementById("tLoan").value;
annuity=(aLoaned*i)/(1-(1/Math.pow(1+i,n)));
document.getElementById("payLoan").value=parseInt(annuity*100)/100;
nB ="<table border='1'>";
nB+=" <tr>";
nB+=" <td>Year</td>";
nB+=" <td>Interest</td>";
nB+=" <td>Principal</td>";
nB+=" <td>Balance</td>";
nB+=" <tr>";
lBalance=aLoaned;
sumInterest=0;
sumPrincipal=0;
for (y=1; y<=n; y++)
{
aInterest=lBalance*i;
aPrincipal=annuity-aInterest;
lBalance-=aPrincipal;
sumInterest+=aInterest;
sumPrincipal+=aPrincipal;
nB+=" <tr>";
nB+=" <td>"+y+"</td>";
nB+=" <td align='right'>"+parseInt(aInterest*100)/100+"</td>";
nB+=" <td align='right'>"+parseInt(aPrincipal*100)/100+"</td>";
nB+=" <td align='right'>"+parseInt(lBalance*100)/100+"</td>";
nB+=" </tr>";
}
nB+=" <tr>";
nB+=" <td>Sum :</td>";
nB+=" <td align='right'>"+parseInt(sumInterest*100)/100+"</td>";
nB+=" <td align='right'>"+parseInt(sumPrincipal*100)/100+"</td>";
nB+=" <td> </td>";
nB+=" </tr>";
nB+="</table>";
document.getElementById("amortTable").innerHTML=nB;
}
</script>
<body>
<div align="center">
<img src="up.png"><br>
<b>ABC Bank of Tacloban</b>
</div><hr>
<table>
<caption>Loan Application Form<hr></caption>
<tr>
<td align="right">Name of borrower :</td>
<td><input type="text" id="nBorr" size="30"></td>
</tr>
<tr>
<td align="right">Amount to be loaned :</td>
<td><input type="text" id="aLoaned" size="12"></td>
</tr>
<tr>
<td align="right">Term :</td>
<td><input type="text" id="tLoan" size="2"> years</td>
</tr>
<tr>
<td align="right">Interest rate :</td>
<td><input type="text" id="iLoan" size="5"> % per annum</td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td align="right">Annuity :</td>
<td><input type="text" id="payLoan" size="12" readonly></td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="Calculate" onClick="calcATable();"></td>
</tr>
</table><br>
<div id="amortTable"></div>
</body>
</html>
I tried to make it work on php. Here's my php code:
Code:
<?php
$b="<html>";
$b.="<script type='text/JavaScript'>";
$b.=" function calcATable()";
$b.=" {";
$b.=" aLoaned=document.getElementById('aLoaned').value;";
$b.=" i=document.getElementById('iLoan').value/100;";
$b.=" n=document.getElementById('tLoan').value;";
$b.=" annuity=(aLoaned*i)/(1-(1/Math.pow(1+i,n)));";
$b.=" document.getElementById('payLoan').value=parseInt(annuity*100)/100;";
$b.=" nB ='<table border='1'>';";
$b.=" nB+=' <tr>';";
$b.=" nB+=' <td>Year</td>';";
$b.=" nB+=' <td>Interest</td>';";
$b.=" nB+=' <td>Principal</td>';";
$b.=" nB+=' <td>Balance</td>';";
$b.=" nB+=' <tr>';";
$b.=" lBalance=aLoaned;";
$b.=" sumInterest=0;";
$b.=" sumPrincipal=0;";
$b.=" for (y=1; y<=n; y++)";
$b.=" {";
$b.=" aInterest=lBalance*i;";
$b.=" aPrincipal=annuity-aInterest;";
$b.=" lBalance-=aPrincipal;";
$b.=" sumInterest+=aInterest;";
$b.=" sumPrincipal+=aPrincipal;";
$b.=" nB+=' <tr>';";
$b.=" nB+=' <td>'+y+'</td>';
$b.=" nB+=' <td align='right'>'+parseInt(aInterest*100)/100+'</td>';";
$b.=" nB+=' <td align='right'>'+parseInt(aPrincipal*100)/100+'</td>';";
$b.=" nB+=' <td align='right'>'+parseInt(lBalance*100)/100+'</td>';";
$b.=" nB+=' </tr>';";
$b.=" }
$b.=" nB+=' <tr>';";
$b.=" nB+=' <td>Sum :</td>';";
$b.=" nB+=' <td align='right'>'+parseInt(sumInterest*100)/100+'</td>';";
$b.=" nB+=' <td align='right'>'+parseInt(sumPrincipal*100)/100+'</td>';";
$b.=" nB+=' <td> </td>';";
$b.=" nB+=' </tr>';";
$b.=" nB+='</table>';";
$b.=" document.getElementById("amortTable").innerHTML=nB;";
$b.=" }";
$b.="</script>";
$b.="<body>";
$b.=" <div align='center'>";
$b.=" <img src='up.png'><br>";
$b.=" <b>ABC Bank of Tacloban</b>";
$b.=" </div><hr>";
$b.=" <table>";
$b.=" <caption>Loan Application Form<hr></caption>";
$b.=" <tr>";
$b.=" <td align='right'>Name of borrower :</td>";
$b.=" <td><input type='text' id='nBorr' size='30'></td>";
$b.=" </tr>";
$b.=" <tr>";
$b.=" <td align='right'>Amount to be loaned :</td>";
$b.=" <td><input type='text' id="aLoaned" size='12'></td>";
$b.=" </tr>";
$b.=" <tr>";
$b.=" <td align='right'>Term :</td>";
$b.=" <td><input type='text' id='tLoan' size='2'> years</td>";
$b.=" </tr>";
$b.=" <tr>";
$b.=" <td align='right'>Interest rate :</td>";
$b.=" <td><input type='text' id='iLoan' size='5'> % per annum</td>";
$b.=" </tr>";
$b.=" <tr>";
$b.=" <td colspan='2'><hr></td>";
$b.=" </tr>";
$b.=" <tr>";
$b.=" <td align='right'>Annuity :</td>";
$b.=" <td><input type='text' id='payLoan' size='12' readonly></td>";
$b.=" </tr>";
$b.=" <tr>";
$b.=" <td colspan='2'><hr></td>";
$b.=" </tr>";
$b.=" <tr>";
$b.=" <td colspan='2'><input type='button' value='Calculate' onClick='calcATable();'></td>";
$b.=" </tr>";
$b.=" </table><br>";
$b.=" <div id='amortTable'></div>";
$b.="</body>";
$b.="</html>";
echo $b;
?>
When I try to open the file, it get a T_STRING error. Please help. What must be the problem with my codes? And can you please help me in revising the codes to make it work well in php?
Thank you in advance!
Big things come in small packages
You asked exactly the same question over at codingforums in your thread there. You were given the answer there. What don't you like about it.
Yes, I did. Sorry, I saw the reply a few minutes after posting here. And the question I asked there was not as detailed as I did here.
However, I was just hoping I could get several answers so I could at least learn various ways of solving the problem I'm encountering.
If this caused any trouble, I deeply apologize.
Big things come in small packages
The solution you were given at codingforums is much better than what you are trying to do now by joining together each line of code in a php string and then echoing it.
All you have to is, as suggested in the other forum, is to give your filename a .php extension and that will tell the web server to treat your page as a php file and so pass it through the php parser and processor on the server before sending the html back to the browser.
you have syntax errors (missing and unmatched quotes) in the php code.
I see no obvious reason to assign all that text to a PHP variable and then echo that variable. Unless you actually need to manipulate the text in some way, just remove all the PHP stuff, as it appears to be serving no purpose (except maybe eating up a millisecond or two of processing time). If you ever do have a reason to assign a bunch of straight HTML text like that to a PHP variable, you'll likely find that the heredoc syntax will save you a lot of grief.
Both of these do the exact same thing:
example1.php:
Code:
<html><head><title>test</title></head><body>
<p>This is a test.</p>
</body></html>
example2.php:
PHP Code:
<?php
$var = "<html><head><title>test</title></head><body>
<p>This is a test.</p>
</body></html>" ;
echo $var ;
So why not use the "example1.php" version if you don't need to do anything else?
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in
Nation
eBookworm.us
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks