PHP calculator on submit cannot find ' php echo server[php self]'

Permalink
Hi There,
I am extremely new to php and I've just coded a bond calculator for my client.
I've used the HTML block to add the code to the page (also copied the files from this posthttps://www.concrete5.org/community/forums/usage/execute-php-code-in... into the blocks directory)
When I test the form and submit it however states it can't find the page <?php echo $_SERVER['PHP_SELF']; ?>
The form needs to display the results on the same page.
How do I get this form to work?
This is a learning curve for me and any guidance will be appreciated.

junebird
 
Gondwana replied on at Permalink Reply
Gondwana
I'd seriously consider doing the calculations in javascript. It could end up being a lot simpler and quicker to see results, and would also reduce server load.
junebird replied on at Permalink Reply
junebird
Unfortunately I have no experience in javascript, I'm only just learning php and then I'm planning on moving on to javascripting
hutman replied on at Permalink Reply
hutman
Couple questions

1) what version of concrete5 are you running?
2) what did you put into the blocks directly exactly?
3) what did you put into the HTML block exactly?

That post you referenced is quite old and the blocks referenced for it will not work with 5.7 or 5.8 so it's possible that you're putting PHP into the HTML block and it's just not doing anything.
junebird replied on at Permalink Reply
junebird
I just added it to the 5.6.3.3 version
So I inserted an HTML block into the page and
This is my code I added inside the HTML block
So when you submit the form it opens up a page say it couldn't find page php echo $_SERVER['PHP_SELF'];
// form input
<? php
$grossincome = $_POST["grossincome"];
$nettincome = $_POST["nettincome"];
$expenses = $_POST["expenses"];
$interest = $_POST["interest"];
$loanterm = $_POST["loanterm"];
$interestformula = $interest/100;
$montlyinterest = $interestformula/12;
$monthlyannuity = (1-(pow((1+$montlyinterest),(-$loanterm*12))))/$montlyinterest;
$affordability = $grossincome*0.3*$monthlyannuity;
$monthlyrepayment = $affordability/$monthlyannuity;
$totalrepayment = $monthlyrepayment*$loanterm*12;
$totalinterestpaid = $totalrepayment-$affordability;
?>
hutman replied on at Permalink Reply
hutman
If you view source on the page do you see <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> ? If you do then then PHP is not being evaluated. And whatever you added to your 5.6.3.3 isn't working.
junebird replied on at Permalink Reply
junebird
It is showing, so how do I then add this calculator I created to a page?
Because it also doesn't work when it's hard coded, it just redirects to the home page once submitted, instead of showing the results.
I also get this warning when its hard coded:
Warning: Division by zero in "sitename" on line 85
hutman replied on at Permalink Reply 1 Attachment
hutman
I would suggest making it into an External Form, I have attached an example for you to start with.

Your division by 0 is going to be an issue if you don't validate the inputted values, you can do that in the controller.

Also, you're going to run into issues because you're calculating on $_POST["nettincome"] and $_POST["interest"] but those fields don't exist on your form.

You can work on this more by putting what's attached (unzipped) into the /blocks directory of you site then adding the Calculator Form - External Form block to your page. So when it's there the directory should look like /blocks/external_form/forms/calculator_form.php and /blocks/external_form/forms/controllers/calculator_form.php