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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LugNut29
    New Member
    • Jul 2013
    • 16

    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

    Code:
    document.write ("<table ><table class=ctable width=50% border='0'>");
    	document.write ("<tr>","<td>");
    		document.write ("<form name=first>");
    		document.write ("<table class=ctable>");
    			document.write ("<tr>","<td>Loan Amount:</td>","<td><input name=aa type=text size=15 
    
    onkeyup=checnum(this) ></td>","</tr>");
    			document.write ("<tr>","<td>Interest Rates:</td>","<td><input name=bb type=text size=15 
    
    onkeyup=checnum(this) ></td>","</tr>");
    			document.write ("<tr>","<td>Term(Years):</td><td><input name=cc type=text size=15 
    
    onkeyup=checnum(this)></td>","</tr>");
    		document.write ("</table>");
    		document.write ("<br>");
    		document.write ("<input type=button name=ss value=calculate onclick=loan() class=calc>");
    		document.write ("<br>");
    
    function checnum(as)
    {
    	var dd = as.value;
    	if(isNaN(dd))
    	{
    		dd = dd.substring(0,(dd.length-1));
    		as.value = dd;
    	}		
    }
    function loan()
    {
    	var a = document.first.aa.value;
    	var b = document.first.bb.value;
    	var c = document.first.cc.value;
    	var n = c * 12;
    	var r = b/(12*100);
    	var p = (a * r *Math.pow((1+r),n))/(Math.pow((1+r),n)-1);
    	var prin = Math.round(p*100)/100;
    	document.first.r1.value = prin;
    	var mon = Math.round(((n * prin) - a)*100)/100;
    	document.first.r2.value = mon;
    	var tot = Math.round((mon/n)*100)/100;
    	document.first.r3.value = tot;
    	for(var i=0;i<n;i++)
    	{
    		var z = a * r * 1;
    		var q = Math.round(z*100)/100;
    		var t = p - z;
    		var w = Math.round(t*100)/100;
    		var e = a-t;
    		var l = Math.round(e*100)/100;
    		a=e;
    	}
    }
    
    
    		document.write ("<br>");
    		document.write ("<table class=ctable border=1 cellspacing=0 cellpadding=3>");
    
    			document.write ("<tr>","<th>Payment</th>","<th>Total Interest Paid</th>","<th>Monthly 
    
    Interest</th>","</tr>");
    			document.write ("<tr>","<td> <input name=r1 type=text readonly > </td>","<td> <input 
    
    name=r2 type=text readonly ></td>","<td> <input name=r3 type=text readonly > </td>","</tr>");
    			document.write ("</td>","</tr>");
    		document.write ("</table>","</form>");
    document.write ("</table>");
    
    
    [B]Main PHP Page[/B]
    
    <?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="generator" content="HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
    
    <title></title>
    <body>
    
    <script src="table.js" type="text/javascript">
    </script>
    
    </body>
    </html>
    Last edited by Rabbit; Aug 6 '13, 03:58 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • LugNut29
      New Member
      • Jul 2013
      • 16

      #3
      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?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        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.

        Comment

        • LugNut29
          New Member
          • Jul 2013
          • 16

          #5
          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:

          Code:
          [B]table.class[/B]
          
          <?php
                  $pay = round(calculatePayment($balance,$rate,$term),2);
                  echo "<tr><th colspan='2' align='center'>Here's Your Estimated Monthly Payment</th></tr>";
          	echo "<tr><td class='res'> Monthly Payments </td><td> $".$pay."</td></tr>";
          	echo "<tr><td class='res'>Interest Paid</td><td> $".(($term*$pay*$period)-$balance)."</td></tr>";
          	echo "<tr><td class='res'>Loan Term</td><td>".$term." Years</td></tr>";
          ?>
          
          [B]calculator.class[/B]
          
          <?php
          $period = 12; 
          
          function calculatePayment($balance,$rate,$term){
             global $period; 
          
             $N = $term * $period; 
             $I = ($rate/100)/$period;
             $v = pow((1+$I),$N);
             $t = ($I*$v)/($v-1);
             $result = $balance*$t;
             
             return $result;
          }
          
          $balance = isset($_POST['balance']) ? $_POST['balance'] : '';
          $rate    = isset($_POST['rate']) ? $_POST['rate'] : '';
          $term    = isset($_POST['term']) ? $_POST['term'] : '';
          
          ?>
          
          [B]Page[/B]
          
          <body>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
                  <div class="CSSTable" ><table>
                    <tr><td>Loan Amount:</td><td><input class="text" name="balance" type="text" size="15" value="<?php echo $balance; ?>" /> $</td></tr>
                    <tr><td>Interest rate:</td><td> <input class="text" name="rate" type="text" size="5" value="<?php echo $rate; ?>" /> %</td></tr>
                    <tr><td>Loan term:</td><td> <input class="text" name="term" type="text" size="5" value="<?php echo $term; ?>" /> years</td></tr>
                    <tr><td align="center" colspan="2"><br/><input class="text" type="submit" name="submitBtn" value="Calculate" /></td></tr>
                  </table> 
                </form>
          <?php    
              if ((isset($_POST['submitBtn']) && ($balance != '') && ($rate != '') && ($term != ''))){     
          ?>
                  <table class="CSSTable">
          <?php
          	require_once('class/table.class.php');
          ?>
                  </table>
          
               </div>
          <?php            
          }
          ?>
          </body>

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Glad you got it working, good luck with the rest of your project.

            Comment

            Working...