duanhuoyao7011 2015-01-22 06:57
浏览 19
已采纳

使用php从tip计算器打印表

I'm trying to display the results from my tip calculator into a table using php, when I enter in the bill amount and tip I want it to have a table of two columns and three rows for the first column I want it to display these results-

bill amount:,tip percent: and total amount: with the results from the tip calculator to show in the other column.

I just need help starting this code off using php so that my results show in a table from what I input, here is my code that I have so far:

 <!DOCTYPE html>
 <form id="calculator">
     <p>Amount: <input id="amount" /></p>
  <p>Tip Percentage: <input id="percentage" /></p>
  <hr />
<p>Tip: <input id="tip" disabled="disabled" /></p>
<p>Total: <input id="total" disabled="disabled" /></p>
<p><input type="submit" /></p>
</form>
</html>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>

  function calculate () {

   var amount = $('#amount').val();
   var percentage = $('#percentage').val();

  var tip = amount * (percentage / 100);


   var total = Number(amount) + tip;

   $('#tip').val( tip.toFixed(2) );
   $('#total').val( total.toFixed(2) );


   return false;
   }

   $('#calculator').submit( calculate );
   </script>
  • 写回答

1条回答 默认 最新

  • doumu5662 2015-01-22 08:04
    关注

    As correctly pointed out in comments, you can do everything with jQuery (without PHP).

    $('#calculator').submit(function()
    {
       var amount = $('#amount').val();
       var percentage = $('#percentage').val();
    
       // do some basic validation
       if(!$.isNumeric(amount) || amount<0 ||
          !$.isNumeric(percentage) || percentage<0)
       {
           $('#prompt').html('Please enter correct values!');
           return false;
       }
    
       var tip = amount * (percentage / 100);
       var total = Number(amount) + tip;
    
       $('#tip').html(tip.toFixed(2));
       $('#total').html(total.toFixed(2));
    
       return false;
    });
    

    Fiddle

    I've just added some basic validation to your input, corrected some layout (i.e. #tip and #total are results, so you can create a result table outside the form), added a prompt for displaying errors and changed val() to html() to update results. Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?