• I have created a new page tempate on my site and added the following code:

    <div id="calculator">
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    How much do you pay for your gas?<br />
    <input name="gas" id="gas" /> Per Month<br /><br />
    How much do you pay for boiler cover?<br />
    <input name="cover" id="cover" /> Per Month<br /><br />
    <input type="submit" /><br />
    </form>
    Preview:<br />
          <?php $gasCost = ($_POST['gas']);
    	  $boilerCoverCost = ($_POST['cover']);
    	  $gasSaving = $gasCost*0.3;
    	  $totalSaving = ($gasCost * 0.3) + $boilerCoverCost; 
    
    	  echo "Gas Saving = $gasSaving <br />Boiler Cover = $boilerCoverCost <br />Total Saving = $totalSaving"; ?>
    </div>

    But, when a user enters data into the 2 form fields and clicks submit, they are just redirected to index.php! Please help

Viewing 1 replies (of 1 total)
  • The problem is with this line:
    <?php echo $_SERVER['PHP_SELF']; ?>
    The value for $_SERVER[‘PHP_SELF’] is the location of the PHP file that’s being used in that operation, and with the WordPress front-end, that’s always index.php

    There is actually a very easy fix for this – remove that piece of code.

    That’s all there is to it. Have the form definition look like this:
    <form action="" method="post">
    Since there’s no action specified the browser will submit the form to the same URL that it’s currently on, so you will be able to do whatever processing you need to there.

Viewing 1 replies (of 1 total)
  • The topic ‘PHP Form in Page Template’ is closed to new replies.