473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to convert HTML/JavaScript page to a PHP page?

Hi,

I am new to programming and PHP and need some help converting this client side page to a server side PHP page. Here's what I have to convert:

Script to be executed

Expand|Select|Wrap|Line Numbers
  1. document.write ("<table ><table class=ctable width=50% border='0'>");
  2.     document.write ("<tr>","<td>");
  3.         document.write ("<form name=first>");
  4.         document.write ("<table class=ctable>");
  5.             document.write ("<tr>","<td>Loan Amount:</td>","<td><input name=aa type=text size=15 
  6.  
  7. onkeyup=checnum(this) ></td>","</tr>");
  8.             document.write ("<tr>","<td>Interest Rates:</td>","<td><input name=bb type=text size=15 
  9.  
  10. onkeyup=checnum(this) ></td>","</tr>");
  11.             document.write ("<tr>","<td>Term(Years):</td><td><input name=cc type=text size=15 
  12.  
  13. onkeyup=checnum(this)></td>","</tr>");
  14.         document.write ("</table>");
  15.         document.write ("<br>");
  16.         document.write ("<input type=button name=ss value=calculate onclick=loan() class=calc>");
  17.         document.write ("<br>");
  18.  
  19. function checnum(as)
  20. {
  21.     var dd = as.value;
  22.     if(isNaN(dd))
  23.     {
  24.         dd = dd.substring(0,(dd.length-1));
  25.         as.value = dd;
  26.     }        
  27. }
  28. function loan()
  29. {
  30.     var a = document.first.aa.value;
  31.     var b = document.first.bb.value;
  32.     var c = document.first.cc.value;
  33.     var n = c * 12;
  34.     var r = b/(12*100);
  35.     var p = (a * r *Math.pow((1+r),n))/(Math.pow((1+r),n)-1);
  36.     var prin = Math.round(p*100)/100;
  37.     document.first.r1.value = prin;
  38.     var mon = Math.round(((n * prin) - a)*100)/100;
  39.     document.first.r2.value = mon;
  40.     var tot = Math.round((mon/n)*100)/100;
  41.     document.first.r3.value = tot;
  42.     for(var i=0;i<n;i++)
  43.     {
  44.         var z = a * r * 1;
  45.         var q = Math.round(z*100)/100;
  46.         var t = p - z;
  47.         var w = Math.round(t*100)/100;
  48.         var e = a-t;
  49.         var l = Math.round(e*100)/100;
  50.         a=e;
  51.     }
  52. }
  53.  
  54.  
  55.         document.write ("<br>");
  56.         document.write ("<table class=ctable border=1 cellspacing=0 cellpadding=3>");
  57.  
  58.             document.write ("<tr>","<th>Payment</th>","<th>Total Interest Paid</th>","<th>Monthly 
  59.  
  60. Interest</th>","</tr>");
  61.             document.write ("<tr>","<td> <input name=r1 type=text readonly > </td>","<td> <input 
  62.  
  63. name=r2 type=text readonly ></td>","<td> <input name=r3 type=text readonly > </td>","</tr>");
  64.             document.write ("</td>","</tr>");
  65.         document.write ("</table>","</form>");
  66. document.write ("</table>");
  67.  
  68.  
  69. Main PHP Page
  70.  
  71. <?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
  72. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  73. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  74.  
  75. <html xmlns="http://www.w3.org/1999/xhtml">
  76. <head>
  77. <meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
  78.  
  79. <title></title>
  80. <body>
  81.  
  82. <script src="table.js" type="text/javascript">
  83. </script>
  84.  
  85. </body>
  86. </html>
Aug 6 '13 #1
5 2118
Rabbit
12,516 Expert Mod 8TB
There's no point to all the document.write calls. Just use the HTML directly.

You can't do an onkeyup event without javascript but you can do the validation on the server side if you want. It's not as responsive as javascript but if you can't use any javascript, that's the only option you have.

As for calculating the loan, submit the loan data to either itself or a different page to display the results. The option is up to you.
Aug 6 '13 #2
Well, I want to have the tables created dynamically, so that's why there are so many document.write. I know php uses the print function, so would I just replace the document.write with the print function? What I want to do is have the table with the calculations appear after the button is pressed. Does that make since? I'm just not sure where to start. Would you be able to give an example of how to do some of this?
Aug 6 '13 #3
Rabbit
12,516 Expert Mod 8TB
If you're not sure where to start, then you need to start with a PHP tutorial. I recommend this one from w3schools: http://w3schools.com/php/.

You can't do "dynamic" with PHP. At least not in the same fashion as javascript. With PHP, you have to submit the data to a PHP page which returns a new page to display.
Aug 6 '13 #4
Thanks for the help. I think I got it. It all seems to work, so I'm going with it. :) This is what I have come up with:

Expand|Select|Wrap|Line Numbers
  1. table.class
  2.  
  3. <?php
  4.         $pay = round(calculatePayment($balance,$rate,$term),2);
  5.         echo "<tr><th colspan='2' align='center'>Here's Your Estimated Monthly Payment</th></tr>";
  6.     echo "<tr><td class='res'> Monthly Payments </td><td> $".$pay."</td></tr>";
  7.     echo "<tr><td class='res'>Interest Paid</td><td> $".(($term*$pay*$period)-$balance)."</td></tr>";
  8.     echo "<tr><td class='res'>Loan Term</td><td>".$term." Years</td></tr>";
  9. ?>
  10.  
  11. calculator.class
  12.  
  13. <?php
  14. $period = 12; 
  15.  
  16. function calculatePayment($balance,$rate,$term){
  17.    global $period; 
  18.  
  19.    $N = $term * $period; 
  20.    $I = ($rate/100)/$period;
  21.    $v = pow((1+$I),$N);
  22.    $t = ($I*$v)/($v-1);
  23.    $result = $balance*$t;
  24.  
  25.    return $result;
  26. }
  27.  
  28. $balance = isset($_POST['balance']) ? $_POST['balance'] : '';
  29. $rate    = isset($_POST['rate']) ? $_POST['rate'] : '';
  30. $term    = isset($_POST['term']) ? $_POST['term'] : '';
  31.  
  32. ?>
  33.  
  34. Page
  35.  
  36. <body>
  37.       <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
  38.         <div class="CSSTable" ><table>
  39.           <tr><td>Loan Amount:</td><td><input class="text" name="balance" type="text" size="15" value="<?php echo $balance; ?>" /> $</td></tr>
  40.           <tr><td>Interest rate:</td><td> <input class="text" name="rate" type="text" size="5" value="<?php echo $rate; ?>" /> %</td></tr>
  41.           <tr><td>Loan term:</td><td> <input class="text" name="term" type="text" size="5" value="<?php echo $term; ?>" /> years</td></tr>
  42.           <tr><td align="center" colspan="2"><br/><input class="text" type="submit" name="submitBtn" value="Calculate" /></td></tr>
  43.         </table> 
  44.       </form>
  45. <?php    
  46.     if ((isset($_POST['submitBtn']) && ($balance != '') && ($rate != '') && ($term != ''))){     
  47. ?>
  48.         <table class="CSSTable">
  49. <?php
  50.     require_once('class/table.class.php');
  51. ?>
  52.         </table>
  53.  
  54.      </div>
  55. <?php            
  56. }
  57. ?>
  58. </body>
Aug 8 '13 #5
Rabbit
12,516 Expert Mod 8TB
Glad you got it working, good luck with the rest of your project.
Aug 8 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sebastien B. | last post by:
I'm looking for the best tool to convert 'every day' html into proper XHTML so that I can parse it as an XML document. So far I've been using Tidylib to do this, but it doesn't handle things as...
0
by: Zhao Sheng | last post by:
http://www.guangmingsoft.com/htmltoimage/help.htm Html To Image Html To Image 2.0 Released! Html To Image help you convert html page from any URL to image or thumbnails easily and quickly. ...
2
by: Stephen Miller | last post by:
I have an ASPX report and I want to capture the rendered HTML and write to a file on the webserver. Several posts suggest using WebRequest to make a second call to the page, and screen-scrape the...
1
by: vaidas gudas | last post by:
how to convert HTML page to XML format
6
by: PenguinPig | last post by:
Dear All Experts I would like to know how to convert a HTML into Image using C#. Or allow me contains HTML code (parsed) in Image? I also tried this way but it just display the character "<" &...
5
by: fjanon | last post by:
Is there a way to remove the default footer/header from the printed page when printing a page from HTML/Javascript in IE 6? I want to use letterhead paper and I have to remove manually the...
7
by: Class | last post by:
Hi all, I am looking for some .NET code which can convert HTML to a PDF. I have looked the internet bad without any luck so far. I have found the iText.NET this has alot of sample but couldn't...
5
by: Damiano | last post by:
Hello, How could i convert Html page in a PDF file? Is there a free library? Thanks
2
by: meenanarayanan | last post by:
Hi, I am new to jasper.I need to convert a jsp page into PDF in jasper.I am getting values from the database in the jsp page.The jsp page will display the values in the table format.The...
7
by: Arsale | last post by:
I am trying to pass a javascript variable to php on javascript generated page, but on the new page I got only "Array". Where did I go wrong? On first page, i've got image map with areas....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.