Softpedia
 

NEWS CATEGORIES:



NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
Home > News > Webmaster > Tips and Tricks

September 25th, 2007, 17:27 GMT · By Catalin Bocanu

A Simple Calculator with HTML Forms and PHP

SHARE:

Adjust text size:


An Online Form Generator
Enlarge picture
The data contained by a HTML form is used by a site owner with various purposes. But before a form data can be used, you must get it from your visitors. There are many ways in which you can obtain the data from the forms. In another article there are presented the methods in which data is sent to be processed on server side.

After the data is processed using server side scripts, it must be displayed, stored in a database, received in email or edited. Usually, these actions are individual, or combined between them are included in the processing script. PHP scripting language offers you simple ways of processing forms and of performing various actions with the form data.

Let's consider a form that requires users to input two numbers and the form processor could use them to perform various mathematical calculations such as addition, substraction and more. The form must be saved in a file form.html and must have the next structure:

CODE
<html>
<title>A simple math calculator</title>
<body>
Insert two numbers in the next form and hit submit button <br>
<form action="calculation.php" method="post">
Firstnumber: <input name="num1" type="text" /><br>
Secondnumber: <input name="num2" type="text" />
<input type="submit" />
</form>
</body>
</html>


Next, we will present the code for the form processor which must be saved as calculation.php

CODE
<html>
<head>
<title>Simple Math With User Input</tite>
</head>
<body>
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$a = $num1 + $num2;
$b = $num1 - $num2;
echo "The sum of the two numbers is ". $a
echo "The difference of the two numbers is ". $b
?>
</body>
</html>


If you open in a web browser the file form.html, fill in two numbers, such as 6 and 5, hit the submit button, then on the server side the form will be processed, the two numbers from the form fields are read and assigned as values to the variables $num1 and $num2, then the value of their difference is assigned to the variable $b and the sum to $a. The last step is the display of sum value (11), difference value (1), in the case of 6 and 5. You can try to add more text input fields to the form and corresponding to them to modify the processing script to display other mathematical operations results.

For example, you can define another form field for a number named num3 (thirdnumber) and other operations between num1, num2 and num3 to compare how the php script output will change. On the other side, you should notice that the form file is of html type and the processing script is php. Depending on the server directives, if you save the form processor file as .html, it may not perform the processing action.

It is recommended to always save your files that contain php code as .php in order to not receive error messages on the server side. You can also try, in time, to develop the capabilities of this simple calculator, by adding trigonometric functions, complex expressions evaluation, form validation and more.


TELL US WHAT YOU THINK:

27,155 hits · 9 comments · Link to this article · Print article · Send to friend · Subscribe to news

MUST-READ RELATED ARTICLES:


The General Mechanism of Forms Processing

How to Insert a Secure Form Mailer in Wordpress

Tips for HTML Forms Design

Could Login Passwords Be Cracked?

Is Your E-mail Secure?

READER COMMENTS:


Comment #1 by: wasted time on 22 Apr 2011, 04:53 UTC reply to this comment

this didn't work. i did everything exactly as stated. and got:

Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in D:\Hosting\....

Comment #1.1 by: nick on 10 Aug 2011, 16:53 GMT

It works if you put ; at the end of each echo line, but I dont get result displayed. If I go to View web site source there is result calculated, but on the web site itself there is nothing to see.

Comment #1.2 by: Jack on 06 Nov 2011, 17:23 GMT

I actually have the same problem as nick: I can see the result in the title of the page and on its source, but the page itself turns out to be empty.

Please advise, thanks!
Jack

PS: of course I put ; at the end of both lines as advised earlier.

Comment #1.3 by: Roger on 08 May 2012, 18:22 GMT

I found the error - in the closing tag of calculation.php then title is not spelled correctly.

Once you change that then it does display the answer text correctly.


Comment #2 by: Annie on 25 May 2011, 02:43 UTC reply to this comment

Can anyone help me with a simple php calculator?

It only has one numerical input field I should think it would take someone who knows how under 10 mins to create the code which I would just want to paste into wordpress page...

The customer types in a number (relating to years in this case)

i.e

Over ____ Years

and then then hits CALCULATE.

Answer ___________

The no. of years must then be multiplied by £1500 with an additional £1500 being added on top. i.e 10 years = £16,500

Comment #2.1 by: yueh han on 17 Jul 2011, 07:25 GMT

thats simple.
using the above form..
$num1 = $_POST['num1'];
$sum= $num1 * 1500 + 1500;
echo "The answer is ". $sum;

** to solve the error above just add the ";" behind each echo. it should run properly now


Comment #3 by: kumar on 03 Sep 2011, 13:40 UTC reply to this comment

Very Good and useful

Comment #3.1 by: Marco on 06 Nov 2011, 17:29 GMT

Yes, it works... but the output page is empty, you can only see the result on the title page! There must be a way to correct that... what do you reckon?


Comment #4 by: el em on 02 May 2012, 06:47 UTC reply to this comment

what php codes are for me to compute my imputed fields on my DB?

Copyright © 2001-2012 Softpedia. Contact/Tip us at

WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM