douyouchou1085 2013-05-20 22:41
浏览 43

php表单计算器如何解决这个问题

OK i am making a form that calculates line 1+ line 2 +*.01% then adds line 3+ line 4 then gives a total. I am having a problem with *.01 it works with just the first two but when i add the last two it drops one line if i take it out all four add fine.

<?php
if (count($_POST) > 0 && isset($_POST["calc15TextArea2"])){
    // add First and Second Numbers
    $sum = $_POST["indexEditbox1"] + $_POST["indexEditbox2"]*.01+ $_POST      ["calc15Editbox3"] + $_POST ["calc15Editbox4"];
    // their sum is diplayed as
    echo "Total is $ $sum";
}
?>
  • 写回答

2条回答 默认 最新

  • dsk88199 2013-05-20 22:45
    关注

    wrap all your post vars in intval() like eg intval($_POST['indexEditBox2'])

    this function will get the integer value out of the string. (use the equivalent functions such as floatval() if they are not integers)

    edit: Also try 0.01 in stead of .01. Given that this just is a wild hunch, I just might be that the .. is picked up as the concatenation operator.

    so try:

        $sum = floatval( $_POST["indexEditbox1"] ) + floatval( $_POST["indexEditbox2"] ) * 0.01+ floatval( $_POST["calc15Editbox3"] ) + floatval( $_POST["calc15Editbox4"] ) ;
    

    edit2: See the comments! multiplications and divisions take precedence over adding and subtracting.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端