非常抱歉,上次说写一个php按钮计算器,因为最近比较忙,所以比较忙,今天撸了出来,个人还是觉得比较好玩的,基本上是完善了,下面贴贴代码。
php按钮计算器制作目录
按钮计算器的html代码
按钮计算器的css代码
按钮计算器的php代码
php按钮计算器的完整代码
按钮计算器的html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP按钮计算器怎么写?</title>
</head>
<body>
<form action="" method="post">
<div>
<input type="button" value="1" onclick="document.getElementById('result').value += '1'">
<input type="button" value="2" onclick="document.getElementById('result').value += '2'">
<input type="button" value="3" onclick="document.getElementById('result').value += '3'">
<input type="button" value="+" onclick="document.getElementById('result').value += '+'">
<br>
<input type="button" value="4" onclick="document.getElementById('result').value += '4'">
<input type="button" value="5" onclick="document.getElementById('result').value += '5'">
<input type="button" value="6" onclick="document.getElementById('result').value += '6'">
<input type="button" value="-" onclick="document.getElementById('result').value += '-'">
<br>
<input type="button" value="7" onclick="document.getElementById('result').value += '7'">
<input type="button" value="8" onclick="document.getElementById('result').value += '8'">
<input type="button" value="9" onclick="document.getElementById('result').value += '9'">
<input type="button" value="×" onclick="document.getElementById('result').value += '×'">
<br>
<input type="button" value="C" onclick="document.getElementById('result').value = ''">
<input type="button" value="0" onclick="document.getElementById('result').value += '0'">
<input type="submit" value="=" name="submit">
<input type="button" value="÷" onclick="document.getElementById('result').value += '÷'">
</div>
<br>
<div>
<input type="text" id="result" name="operation" value="">
<input type="hidden" name="num1" value="">
<input type="hidden" name="num2" value="">
<input type="hidden" name="operator" value="">
</div>
<input type="submit" value="计算" name="submit">
<h2><?php echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8'); ?></h2>
</form>
</body>
</html>
按钮计算器的css代码
body {
font-family: 'Arial', sans-serif;
background-color: #f7f7f7;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #333;
}
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
text-align: center;
}
input[type=button], input[type=submit] {
background-color: #008CBA;
border: none;
color: white;
padding: 10px 22px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 5px 2px;
transition: background-color 0.3s;
cursor: pointer;
border-radius: 5px;
}
input[type=button]:hover, input[type=submit]:hover {
background-color: #005f6b;
}
input[type=text] {
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
width: 80%;
border-radius: 5px;
}
h2 {
height: 20px;
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
}
.calc-container {
border: 1px solid #ccc;
padding: 15px;
border-radius: 5px;
width: 250px;
}
按钮计算器的php代码
<?php
$result = "";
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["operation"])) {
$operation = $_POST["operation"];
// Replace the mathematical symbols with PHP operators
$operation = str_replace(['×', '÷'], ['*', '/'], $operation);
// Validate the input to contain only numbers and allowed operators
if (preg_match("/^[0-9+\-*.\/() ]+$/", $operation)) {
// Calculate the result safely using eval
try {
// Make sure we're using valid PHP code
eval("\$fResult = $operation;");
// Safely escape the result to output
$result = htmlspecialchars($fResult);
} catch (ParseError $e) {
$result = "表达式无效";
}
} else {
$result = "输入无效";
}
}
?>
php按钮计算器的完整代码
<?php
/*
Template Name: 按钮计算器
Template Post Type: post
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP按钮计算器怎么写?</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f7f7f7;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #333;
}
form {
background-color: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
text-align: center;
}
input[type=button], input[type=submit] {
background-color: #008CBA;
border: none;
color: white;
padding: 10px 22px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 5px 2px;
transition: background-color 0.3s;
cursor: pointer;
border-radius: 5px;
}
input[type=button]:hover, input[type=submit]:hover {
background-color: #005f6b;
}
input[type=text] {
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
width: 80%;
border-radius: 5px;
}
h2 {
height: 20px;
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
}
.calc-container {
border: 1px solid #ccc;
padding: 15px;
border-radius: 5px;
width: 250px;
}
</style>
</head>
<body>
<?php
$result = "";
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["operation"])) {
$operation = $_POST["operation"];
// Replace the mathematical symbols with PHP operators
$operation = str_replace(['×', '÷'], ['*', '/'], $operation);
// Validate the input to contain only numbers and allowed operators
if (preg_match("/^[0-9+\-*.\/() ]+$/", $operation)) {
// Calculate the result safely using eval
try {
// Make sure we're using valid PHP code
eval("\$fResult = $operation;");
// Safely escape the result to output
$result = htmlspecialchars($fResult);
} catch (ParseError $e) {
$result = "表达式无效";
}
} else {
$result = "输入无效";
}
}
?>
<form action="" method="post">
<div>
<input type="button" value="1" onclick="document.getElementById('result').value += '1'">
<input type="button" value="2" onclick="document.getElementById('result').value += '2'">
<input type="button" value="3" onclick="document.getElementById('result').value += '3'">
<input type="button" value="+" onclick="document.getElementById('result').value += '+'">
<br>
<input type="button" value="4" onclick="document.getElementById('result').value += '4'">
<input type="button" value="5" onclick="document.getElementById('result').value += '5'">
<input type="button" value="6" onclick="document.getElementById('result').value += '6'">
<input type="button" value="-" onclick="document.getElementById('result').value += '-'">
<br>
<input type="button" value="7" onclick="document.getElementById('result').value += '7'">
<input type="button" value="8" onclick="document.getElementById('result').value += '8'">
<input type="button" value="9" onclick="document.getElementById('result').value += '9'">
<input type="button" value="×" onclick="document.getElementById('result').value += '×'">
<br>
<input type="button" value="C" onclick="document.getElementById('result').value = ''">
<input type="button" value="0" onclick="document.getElementById('result').value += '0'">
<input type="submit" value="=" name="submit">
<input type="button" value="÷" onclick="document.getElementById('result').value += '÷'">
</div>
<br>
<div>
<input type="text" id="result" name="operation" value="">
<input type="hidden" name="num1" value="">
<input type="hidden" name="num2" value="">
<input type="hidden" name="operator" value="">
</div>
<input type="submit" value="计算" name="submit">
<h2><?php echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8'); ?></h2>
</form>
</body>
</html>