本文章向大家介绍一个网页版的计算器,该计算器使用到表单提交数据的相关知识,并且表单选择post方式提交。请看下面实例:
html代码:
<html>
<head>
< >php 使用post方式提交表单的实例</ >
</head>
<body>
<form action = \"calc.php\" method = \"post\">
Value 1: <input type = \"text\" name = \"val1\" size = \"10\">
Value 2: <input type = \"text\" name = \"val2\" size = \"10\">
<br>
Calculation: <br>
<input type = \"radio\" name = \"calc\" value = \"add\"> Add
<input type = \"radio\" name = \"calc\" value = \"sub\"> Subtract
<input type = \"radio\" name = \"calc\" value = \"mul\"> Multiply
<input type = \"radio\" name = \"calc\" value = \"div\"> Divide
<hr>
<input type = \"submit\" value = \"Calculate\">
<input type = \"reset\" value = \"Clear\">
</form>
</body>
</html>
calc.php代码如下:
<?php
$val1 = $_POST[\'val1\'];
$val2 = $_POST[\'val2\'];
$calc = $_POST[\'calc\'];
if( is_numeric( $val1 ) && is_numeric( $val2 ) )
{
if( $calc != null )
{
switch( $calc )
{
case \"add\" : $result = $val1 + $val2; break;
case \"sub\" : $result = $val1 - $val2; break;
case \"mul\" : $result = $val1 * $val2; break;
case \"div\" : $result = $val1 / $val2; break;
}
echo( \"Calculation result: $result\" );
}
}
else
{
echo( \"Invalid entry - please retry\" );
}
?> 继续阅读与本文标签相同的文章
上一篇 :
php 使用数组的方式提交表单form数据
-
新款iphone11已经发布有些日子,这款手机好不好用呢?用数据告诉你
2026-05-14栏目: 教程
-
超进化,时尚玩主的全新一代名爵ZS,起步只要7.98万
2026-05-14栏目: 教程
-
微信 7.0.8 内测新功能,教你如何激活它
2026-05-14栏目: 教程
-
美女机器人刚上市就售罄,除了生孩子,其他什么都能干!
2026-05-14栏目: 教程
-
互联网之光大会的黑科技,总有一款惊艳你!
2026-05-14栏目: 教程
