EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses
  • A calculator UI on your WordPress site (HTML + CSS + JS)

  • Store calculation history or data in a SQL database (MySQL, which WordPress uses)

  • Ability to run calculations with data stored/retrieved from the database


Step 1: Calculator Frontend

(HTML + CSS + JS)

You can embed this code into a WordPress page using the Custom HTML block or a child theme template.

<!– Calculator HTML –>
<div id=”calculator”>
<input type=”text” id=”display” disabled />
<br/>
<button onclick=”appendNumber(‘1’)”>1</button>
<button onclick=”appendNumber(‘2’)”>2</button>
<button onclick=”appendNumber(‘3’)”>3</button>
<button onclick=”setOperation(‘+’)”>+</button>
<br/>
<button onclick=”appendNumber(‘4’)”>4</button>
<button onclick=”appendNumber(‘5’)”>5</button>
<button onclick=”appendNumber(‘6’)”>6</button>
<button onclick=”setOperation(‘-‘)”>-</button>
<br/>
<button onclick=”appendNumber(‘7’)”>7</button>
<button onclick=”appendNumber(‘8’)”>8</button>
<button onclick=”appendNumber(‘9’)”>9</button>
<button onclick=”setOperation(‘*’)”>*</button>
<br/>
<button onclick=”clearDisplay()”>C</button>
<button onclick=”appendNumber(‘0’)”>0</button>
<button onclick=”calculate()”>=</button>
<button onclick=”setOperation(‘/’)”>/</button>
</div>

<!– Basic CSS –>
<style>
#calculator {
width: 160px;
}
#display {
width: 150px;
height: 30px;
font-size: 18px;
text-align: right;
margin-bottom: 5px;
}
button {
width: 35px;
height: 35px;
margin: 2px;
font-size: 18px;
}
</style>

<!– JavaScript for calculator logic –>
<script>
let currentInput = ”;
let operation = null;
let previousInput = ”;

function appendNumber(num) {
currentInput += num;
updateDisplay();
}

function setOperation(op) {
if (currentInput === ”) return;
if (previousInput !== ”) {
calculate();
}
operation = op;
previousInput = currentInput;
currentInput = ”;
}

function calculate() {
if (operation === null || currentInput === ” || previousInput === ”) return;
let a = parseFloat(previousInput);
let b = parseFloat(currentInput);
let result = 0;

switch(operation) {
case ‘+’: result = a + b; break;
case ‘-‘: result = a – b; break;
case ‘*’: result = a * b; break;
case ‘/’:
if (b === 0) {
alert(‘Cannot divide by zero’);
clearDisplay();
return;
}
result = a / b;
break;
}

currentInput = result.toString();
operation = null;
previousInput = ”;
updateDisplay();

// TODO: send calculation and result to server (database)
}

function clearDisplay() {
currentInput = ”;
previousInput = ”;
operation = null;
updateDisplay();
}

function updateDisplay() {
document.getElementById(‘display’).value = currentInput;
}
</script>

Step 2: Connect Calculator to SQL Database in WordPress

How?

  • WordPress uses MySQL.

  • To save calculations, you need a PHP script that accepts data via AJAX and inserts into the database.

  • You can create a custom WordPress plugin or add code to your theme’s functions.php file.

  • Use WordPress’s $wpdb class for database operations safely.


Example PHP code snippet to save calculations (put in functions.php):

add_action(‘wp_ajax_save_calculation’, ‘save_calculation’);
add_action(‘wp_ajax_nopriv_save_calculation’, ‘save_calculation’);

function save_calculation() {
global $wpdb;

$expression = sanitize_text_field($_POST[‘expression’]);
$result = sanitize_text_field($_POST[‘result’]);

$table_name = $wpdb->prefix . ‘calculator_history’; // custom table

$wpdb->insert(
$table_name,
[
‘expression’ => $expression,
‘result’ => $result,
‘created_at’ => current_time(‘mysql’),
],
[
‘%s’,
‘%s’,
‘%s’,
]
);

wp_send_json_success(‘Saved’);
}

Create the custom table (run this once):

function create_calculator_table() {
global $wpdb;

$table_name = $wpdb->prefix . ‘calculator_history’;
$charset_collate = $wpdb->get_charset_collate();

$sql = “CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
expression text NOT NULL,
result text NOT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) $charset_collate;”;

require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta($sql);
}
register_activation_hook(__FILE__, ‘create_calculator_table’);

Step 3: Send Calculation Data to PHP via AJAX

Add this JS code in your calculator script, after calculate() function completes:

function sendCalculation(expression, result) {
fetch(‘<?php echo admin_url(“admin-ajax.php”); ?>’, {
method: ‘POST’,
headers: { ‘Content-Type’: ‘application/x-www-form-urlencoded’ },
body: `action=save_calculation&expression=${encodeURIComponent(expression)}&result=${encodeURIComponent(result)}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
console.log(‘Calculation saved!’);
}
});
}

// Modify calculate() function to call sendCalculation
function calculate() {
// existing code …

currentInput = result.toString();
operation = null;
previousInput = ”;
updateDisplay();

let expression = previousInput + ‘ ‘ + operation + ‘ ‘ + currentInput; // build expression string
sendCalculation(expression, currentInput); // send to server
}

Summary

What you need to do Notes
Create calculator frontend (HTML+CSS+JS) You can add this code
Write PHP AJAX handler to save data Use WordPress hooks and $wpdb
Create a database table to store calculations Use dbDelta and register_activation_hook
Connect JS AJAX to PHP to store results Use fetch or jQuery AJAX
Simple calculator using html

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer