execute PHP form on WordPress page

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”]).

Read More

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!

Related posts

3 comments

  1. It is better to write your php code in functions.php and create shortcode of that and use that shortcode in page. Example:

    add_shortcode( 'multiple_enquiry_form', 'enquiry_form' );
    function enquiry_form(){
        if ( is_user_logged_in() ) { 
            if(isset($_POST['enquiry_form_submit'])){   
        } ?>    
    
        <div class="col-md-4">
            <form method="post" name="enquiry_form" action='<?php echo get_permalink( get_page_by_path( 'enquiry' ) ); ?>'> 
                <div class="form-group">
                <label for="">Project</label>
                <select id='proGroup' name="proGroupForm">
                <option value="">- Select Project -</option>                
                </select>
                </div>
                <div class="form-group">
                <label for="">Message</label>
                <textarea name="enq_message"></textarea>
    
                </div>
                <div class="form-group">
                <label for="">Contact</label>   
                <input type="text" name="contactNumber" placeholder="Contact Number"> 
                </div>
                <div class="form-group">
                <label for="">From</label>  
                <input type="text" name="from_date" class="datepicker" placeholder="From Date"> 
                </div>
                <div class="form-group">
                <label for="">To</label>    
                <input type="text" name="to_date" class="datepicker" placeholder="To Date"> 
                </div>
                <div class="form-group">
                <input type="submit" name="enquiry_form_submit" value="Send" ng-click="submitted=true">
                </div>
            </form>
        </div>
        <?php }
    }
    
  2. 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…

  3. 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/

Comments are closed.