please excuse my English – I have created a _POST form where the user can fill in two numerical fields, which are then multiplied and divided by some constants to output the result when the form is submitted. I am executing the form on the same page using
($_SERVER[“PHP_SELF”]);
The code is very basic and works on the servers where I tested it (can copy and paste if required).
Now I’d like to use this small “calculator” on a WordPress page (I am running Avada theme) but I’m at a complete loss as to how to substitute ($_SERVER[“PHP_SELF”]).
I have tried:
– copying and pasting the whole snippet as it is in the Code Block of fusion builder > I get an error
– storing the variables and calculations in a separate .php file to be called – hardcoded or using bloginfo() funtion
– creating a new template page with the code embedded
…but when I submit the form I am only redirected to the additional php file or to the homepage.
Am I trying to do something completely wrong?
Any help is greatly appreciated as I really don’t understand how this could work…
Many thanks!
It is better to write your php code in functions.php and create shortcode of that and use that shortcode in page. Example:
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=144PHP_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 andREQUEST_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.
Which effectively gives you:
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:
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…
Let’s hope I understand you correct.
You can’t run PHP inside a WordPress post or page. It’s not built that way.
Create a shortcode inside your
functions.php
. This allows you to call functions from within posts and pages. You’ve might have seen them. Looks like[gallery limit="5"]
http://www.smashingmagazine.com/2012/05/wordpress-shortcodes-complete-guide/
If your REALLY want to run PHP inside your page. Check out https://wordpress.org/plugins/insert-php/