完成效果图如下:
实现的功能:
1.简单的加减乘除和取余;
2.判别输入是否为空或不为数字,并弹出提示框给与提示;
操作过程:
新建一个jsq.php,直接把代码贴进去打开网站就可以了;
也就是说,PHP既可以做前端,也可以做后台的事;
<html>
<head> <meta charset="UTF-8"> <title>简单计算器</title> </head> <body> <?php error_reporting(E_ALL & ~E_NOTICE); if($_POST['num1'] == "") echo "<script>alert('第一个数字不能为空')</script>"; elseif(!is_numeric($_POST['num1'])) echo "<script>alert('第一个数字不为数字')</script>"; if($_POST['num2'] == "") echo "<script>alert('第二个数字不能为空')</script>"; elseif(!is_numeric($_POST['num2'])) echo "<script>alert('第二个数字不为数字')</script>"; $sum = ""; $fuhao = $_POST['fuhao']; if ($fuhao == '+') $sum = $_POST['num1'] + $_POST['num2']; if ($fuhao == '-') $sum = $_POST['num1'] - $_POST['num2']; if ($fuhao == '*') $sum = $_POST['num1'] * $_POST['num2']; if ($fuhao == '/') $sum = $_POST['num1'] / $_POST['num2']; if ($fuhao == '%') $sum = $_POST['num1'] % $_POST['num2']; ?> <table border="0" width="400" align="center"> <form action="jsq.php" method="post"> <caption><h1>简单计算器</h1></caption> <tr> <td> <input type="text" size="5" name="num1" value="<?php echo $_POST['num1'] ?>"/> </td> <td> <select name="fuhao"> <option value="+"> + </option> <option value="-"> - </option> <option value="*"> * </option> <option value="/"> / </option> <option value="%"> % </option> </select> </td> <td> <input type="text" name="num2" size="5" value="<?php echo $_POST['num2'] ?>"/> </td> <td> = </td> <td> <input type="text" size="5" value="<?php echo $sum; ?>"/> </td> <tr align="center"> <td> <input type="submit" value="计算" name="submit"> </td> </tr> </form> </table> </body> </html>