PHP custom calculator script, wordpress integration

I’m a complete newbie when it comes to PHP, however I’ve recently been attempting to re-create a calculator application as a WordPress plugin, however I’ve failed horribly.

This is the original code before attempting to convert it into a plugin.

Read More
<?php
    if (isset($_POST['person1'])) $person1 = $_POST['person1'];
    if (isset($_POST['person2'])) $person2 = $_POST['person2'];
    $multivar = 4;
    $answer = ($person1 + $person2) * $multivar;

    echo <<<_END
    <form method='post' action='index.php'>
    <table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
    <tr class="calcheading"><td colspan="2"><strong>How much can you borrow?</strong></td></tr>
    <tr class="calcrow"><td>Person 1 income:</td><td align="center"><input type='text' name='person1' value="$person1"/></td></tr>
    <tr class="calcrow2"><td>Person 2 income</td><td align="center"><input type='text' name='person2' value="$person2"/></td></tr>
    <tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
    _END;
    ?>

    <tr class="calcrow">
    <td><i>You can borrow up to:</td>
    <td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
    </tr>
    </table>
    </form> 

That code in itself is all fine, it works exactly how I need it to work, however when implementing it into a WordPress page is failed. I attempted to create a page template and have that script within that, but with no luck (as it’s quick complex code in the theme I’ve had made for me). So I decided the best route would be to convert it into a plugin and then relay it onto the WordPress page with a shortcode, so I did a bit of reading into it and attempted to do it:

    <?php
/*
Plugin Name: calc
Plugin URI: 
Description: Calculates loans/how much can be borrowed
Version: 1.0
Author: Mikey
Author URI: 
*/

//WordPress Hooks 
add_shortcode('bcalc', 'run_bc'); 


function run_bc() 
{ 
    if (isset($_POST['bc_person1'])) $bc_person1 = $_POST['bc_person1'];
    if (isset($_POST['bc_person2'])) $bc_person2 = $_POST['bc_person2'];
    $bc_multivar = 4;
    $bc_answer = ($bc_person1 + $bc_person2) * $bc_multivar;
}

function bc_getForm() 
{ 

echo "<form method='post' action='".get_permalink()."'>";
echo "<table border='0' width='500px' cellpadding='3' cellspacing='1' class='table'>";
echo "<tr class='calcheading'><td colspan="2"><strong>How much can you borrow?</strong></td></tr>";
echo "<tr class='calcrow'><td>Person 1 income:</td>";
echo "<td align='center'><input type='text' name='bc_person1' value='$bc_person1'/></td></tr>";
echo "<tr class='calcrow2'><td>Person 2 income</td><td align='center'><input type='text' name='bc_person2' value='$bc_person2'/></td></tr>";
echo "<tr class='submit'><td colspan="2"><input type='submit' value='Calculate'/></td></tr>";
echo "<tr class='calcrow'>";
echo "<td><i>You can borrow up to:</td>";
echo "<td align='center'><input type='text' value='.echo round($bc_answer)'></td></i>";
echo "</tr>";
echo "</table>";
echo "</form>";
}

bc_getForm();

?>

I already understand my code is horrible, I’m not asking to really be spoon-fed neither, I just need some form of guidance to assist me in working out how to do this correctly.

Thank you for your time

Related posts

Leave a Reply

1 comment

  1. This should start you off!

    <?php
    /*
    Plugin Name: calc
    Plugin URI: 
    Description: Calculates loans/how much can be borrowed
    Version: 1.0
    Author: Mikey
    Author URI: 
    */
    
    //WordPress Hooks 
    add_shortcode('bcalc', 'run_bc'); 
    
    
    function run_bc() 
    { 
        if (isset($_POST['bc_person1'])) $bc_person1 = $_POST['bc_person1'];
        if (isset($_POST['bc_person2'])) $bc_person2 = $_POST['bc_person2'];
        $bc_multivar = 4;
        $bc_answer = ($bc_person1 + $bc_person2) * $bc_multivar;
    
    ?>
        <form method='post' action='<?php echo get_permalink(); ?>'>
            <table border='0' width='500px' cellpadding='3' cellspacing='1' class='table'>
                <tr class='calcheading'><td colspan='2'><strong>How much can you borrow?</strong></td></tr>
                <tr class='calcrow'><td>Person 1 income:</td>
                <td align='center'><input type='text' name='bc_person1' value='<?php echo $bc_person1 ?>'/></td></tr>
                <tr class='calcrow2'><td>Person 2 income</td><td align='center'><input type='text' name='bc_person2' value='<?php echo $bc_person2 ?>'/></td></tr>
                <tr class='submit'><td colspan='2'><input type='submit' value='Calculate'/></td></tr>
                <tr class='calcrow'>
                <td><i>You can borrow up to:</td>
                <td align='center'><input type='text' value='<?php echo round($bc_answer); ?>'></td></i>
                </tr>
            </table>
        </form>
    
    <?php
    }