I have just tested this on a WordPress page template.
Make sure it is not put inside the loop!
I have put a simple calculation and when you submit the number input to this the result is echoed - that can obviously have a span put around it or whatever it needs. The calculation could have whatever you want in it.
You should also be able to include
your calculation sheet:
include "calculations.php";
These are some security considerations surrounding the use of includes
http://green-beast.com/blog/?p=144
PHP_SELF went to index.php as that is the master template.
You need to use <?php echo $_SERVER['REQUEST_URI']; ?>
if you want to use that for the action to make it work fully programatically, but the hard-coded URL should work (not best practice though and REQUEST_URI
does work).
If you use it you need to use the built in WordPress escape for the URI to sanitize it as you will be "getting" whatever is in it.
<form name="form" action ="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>" method="post">
Which effectively gives you:
<form name="form" action ="http://the full url of the page this script is on/" method="post">
<input type="number" name="number_in">
<input type="submit" name="submit">
</form>
<?php $result = (int) $_POST['number_in'] * 4;
echo $result;
?>
If you wanted it to consecutively add to the result on each form submission you might need to get a bit trixy with a hidden input or session variable - you can just use:
$_SESSION['number_in'] = $_SESSION['number_in'] + $result;
echo $_SESSION['number_in'];
to keep a running tally of cumulative results, as sessions are already active for WordPress.
The use of (int) performs a type conversion - if you know it will be an integer - otherwise float should do. This should avoid the need to clean up the input any further. Might be a handy reference on php.net http://php.net/manual/en/language.types.integer.php
This explains basic usage for hidden inputs.
Get and echo inputs from textarea repeatedly
You could, in principle, just leave tha action blank and it will submit to itself action=""
but here is a good discussion of why it may not be such a good idea to do that. Though it will work, it opens you up to hijacking and inject attack. stackoverflow.com/questions/1131781/
Though you can run php code in WordPress, whether you should or not is a whole different discussion...