用PHP写出计算器
程序员文章站
2022-04-29 17:38:20
......
<body> <?php if (!empty($_post)) { $op=$_post['point']; $sum1 = $_post['sum1']; $sum2 = $_post['sum2']; $sum = 0; if ($sum1 != '' && is_numeric($sum1) && $sum2 != '' && is_numeric($sum2)) { switch ($op) { case '+': $sum = $sum1 + $sum2; break; case '-': $sum = $sum1 - $sum2; break; case '*': $sum = $sum1 * $sum2; break; case '/': $sum = $sum1 / $sum2; break; } echo <<<shi <script> window.onload = function() { document.getelementsbyname('sum3')[0].setattribute('value', '$sum') document.getelementsbyname('sum1')[0].setattribute('value', '$sum1') document.getelementsbyname('sum2')[0].setattribute('value', '$sum2') document.getelementsbyname('{$op}')[0].setattribute('selected','selected') } </script> shi; } else { echo '输入内容必须是数字'; } }; ?> <form action="" method='post'> <input type="text" name="sum1"> <select name='point'> <option name='+'>+</option> <option name='-'>-</option> <option name='*'>*</option> <option name='/'>/</option> </select> <input type="text" name='sum2'> <input type="submit" name='button' value='='> <input type="text" name='sum3'> </form> </body>