/    Sign up×
Community /Pin to ProfileBookmark

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>&nbsp;</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>
[/CODE]

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>&nbsp;</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;
?>
[/CODE]

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! ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@tirnaFeb 19.2011 — 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.
Copy linkTweet thisAlerts:
@dlep2x2authorFeb 19.2011 — 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.
Copy linkTweet thisAlerts:
@tirnaFeb 19.2011 — 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.
Copy linkTweet thisAlerts:
@tirnaFeb 19.2011 — you have syntax errors (missing and unmatched quotes) in the php code.
Copy linkTweet thisAlerts:
@NogDogFeb 19.2011 — 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:
<i>
</i>&lt;html&gt;&lt;head&gt;&lt;title&gt;test&lt;/title&gt;&lt;/head&gt;&lt;body&gt;
&lt;p&gt;This is a test.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;

example2.php:
[code=php]
<?php
$var = "<html><head><title>test</title></head><body>
<p>This is a test.</p>
</body></html>";
echo $var;
[/code]

So why not use the "example1.php" version if you don't need to do anything else?
×

Success!

Help @dlep2x2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...