Display results from a form not showing

I have a form with one input (profit)

I also have calculations that work out how much tax to pay and calculates ni contributions

this form works ok except for one small problem.

Example if you input 60000 into the input box and submit the results should echo

Estimated Tax = £11500
Class 2 National Insurance = £159.00
Class 4 National Insurance Low Rate = £3645.00
Class 4 National Insurance High Rate = £200.00

At the moment it shows

Estimated Tax = £11500
Class 2 National Insurance = £159.00

Class 4 National Insurance High Rate = £200.00

it does not show Class 4 National Insurance Low Rate = £3645.00

also how do i then add the totals up to display a grand total for example

Estimated Tax = £11500
Class 2 National Insurance = £159.00
Class 4 National Insurance Low Rate = £3645.00
Class 4 National Insurance High Rate = £200.00

Grand Total = £15504

here is the form action page …

<?php

if(isset($_POST["profit"])){$profit =($_POST["profit"]);}

$nilowrate=$nilowrate;
$nihighrate=$nihighrate;

//Class 4 Low Rate
$nilow=$profit-9500;
$nilowrate=$nilow/100*9;


//Class 4 High Rate
$nihigh=$profit-50000;
$nihighrate=$nihigh/100*2;


// Less Than 50,000
$profit1=$profit-12500;
$profit1calc=$profit1/100*20;

// Between 50,001 and 100,000
$profit2=$profit-50000;
$profit2a=$profit2/100*40;
$profit2calc=$profit2a+7500;

// Between 100,000 and 101,000
$profit3=$profit-49500;
$profit3a=$profit3/100*40;
$profit3calc=$profit3a+7500;

// Between 101,000 and 102,000
$profit4=$profit-49000;
$profit4a=$profit4/100*40;
$profit4calc=$profit4a+7500;

// Between 102,000 and 103,000
$profit5=$profit-48500;
$profit5a=$profit5/100*40;
$profit5calc=$profit5a+7500;

// Between 103,000 and 104,000
$profit6=$profit-48000;
$profit6a=$profit6/100*40;
$profit6calc=$profit6a+7500;

// Between 104,000 and 105,000
$profit7=$profit-47500;
$profit7a=$profit7/100*40;
$profit7calc=$profit7a+7500;

// Between 105,000 and 106,000
$profit8=$profit-47000;
$profit8a=$profit8/100*40;
$profit8calc=$profit8a+7500;

// Between 106,000 and 107,000
$profit9=$profit-46500;
$profit9a=$profit9/100*40;
$profit9calc=$profit9a+7500;

// Between 107,000 and 108,000
$profit10=$profit-46000;
$profit10a=$profit10/100*40;
$profit10calc=$profit10a+7500;

// Between 108,000 and 109,000
$profit11=$profit-45500;
$profit11a=$profit11/100*40;
$profit11calc=$profit11a+7500;

// Between 109,000 and 110,000
$profit12=$profit-45000;
$profit12a=$profit12/100*40;
$profit12calc=$profit12a+7500;

// Between 110,000 and 111,000
$profit13=$profit-44500;
$profit13a=$profit13/100*40;
$profit13calc=$profit13a+7500;

// Between 111,000 and 112,000
$profit14=$profit-44000;
$profit14a=$profit14/100*40;
$profit14calc=$profit14a+7500;

// Between 112,000 and 113,000
$profit15=$profit-43500;
$profit15a=$profit15/100*40;
$profit15calc=$profit15a+7500;

// Between 113,000 and 114,000
$profit16=$profit-43000;
$profit16a=$profit16/100*40;
$profit16calc=$profit16a+7500;

// Between 114,000 and 115,000
$profit17=$profit-42500;
$profit17a=$profit17/100*40;
$profit17calc=$profit17a+7500;

// Between 115,000 and 116,000
$profit18=$profit-42000;
$profit18a=$profit18/100*40;
$profit18calc=$profit18a+7500;

// Between 116,000 and 117,000
$profit19=$profit-41500;
$profit19a=$profit19/100*40;
$profit19calc=$profit19a+7500;

// Between 117,000 and 118,000
$profit20=$profit-41000;
$profit20a=$profit20/100*40;
$profit20calc=$profit20a+7500;

// Between 118,000 and 119,000
$profit21=$profit-40500;
$profit21a=$profit21/100*40;
$profit21calc=$profit21a+7500;

// Between 119,000 and 120,000
$profit22=$profit-40000;
$profit22a=$profit22/100*40;
$profit22calc=$profit22a+7500;

// Between 120,000 and 121,000
$profit23=$profit-39500;
$profit23a=$profit23/100*40;
$profit23calc=$profit23a+7500;

// Between 121,000 and 122,000
$profit24=$profit-39000;
$profit24a=$profit24/100*40;
$profit24calc=$profit24a+7500;

// Between 122,000 and 123,000
$profit25=$profit-38500;
$profit25a=$profit25/100*40;
$profit25calc=$profit25a+7500;

// Between 123,000 and 124,000
$profit26=$profit-38000;
$profit26a=$profit26/100*40;
$profit26calc=$profit26a+7500;

// Between 124,000 and 125,000
$profit27=$profit-37500;
$profit27a=$profit27/100*40;
$profit27calc=$profit27a+7500;

// Between 125,000 and 150,000
$profit28=$profit-37500;
$profit28a=$profit28/100*40;
$profit28calc=$profit28a+7500;

// Over 150,000
$profit29=$profit-150000;
$profit29a=$profit29/100*45;
$profit29calc=$profit29a+52500;

if ($profit  < 12500)    {echo "Estimated Tax = &pound; 0";}

elseif ($profit <= 50000) {echo "Estimated Tax = &pound; $profit1calc ";}

elseif ($profit >= 50001 && $profit <= 100000) {echo "Estimated Tax = &pound; $profit2calc ";}

elseif ($profit >= 100001 && $profit <= 101000) {echo "Estimated Tax = &pound; $profit3calc ";}

elseif ($profit >= 101001 && $profit <= 102000) {echo "Estimated Tax = &pound; $profit4calc ";}

elseif ($profit >= 102001 && $profit <= 103000) {echo "Estimated Tax = &pound; $profit5calc ";}

elseif ($profit >= 103001 && $profit <= 104000) {echo "Estimated Tax = &pound; $profit6calc ";}

elseif ($profit >= 104001 && $profit <= 105000) {echo "Estimated Tax = &pound; $profit7calc ";}

elseif ($profit >= 105001 && $profit <= 106000) {echo "Estimated Tax = &pound; $profit8calc ";}

elseif ($profit >= 106001 && $profit <= 107000) {echo "Estimated Tax = &pound; $profit9calc ";}

elseif ($profit >= 107001 && $profit <= 108000) {echo "Estimated Tax = &pound; $profit10calc ";}

elseif ($profit >= 108001 && $profit <= 109000) {echo "Estimated Tax = &pound; $profit11calc ";}

elseif ($profit >= 109001 && $profit <= 110000) {echo "Estimated Tax = &pound; $profit12calc ";}

elseif ($profit >= 110001 && $profit <= 111000) {echo "Estimated Tax = &pound; $profit13calc ";}

elseif ($profit >= 111001 && $profit <= 112000) {echo "Estimated Tax = &pound; $profit14calc ";}

elseif ($profit >= 112001 && $profit <= 113000) {echo "Estimated Tax = &pound; $profit15calc ";}

elseif ($profit >= 113001 && $profit <= 114000) {echo "Estimated Tax = &pound; $profit16calc ";}

elseif ($profit >= 114001 && $profit <= 115000) {echo "Estimated Tax = &pound; $profit17calc ";}

elseif ($profit >= 115001 && $profit <= 116000) {echo "Estimated Tax = &pound; $profit18calc ";}

elseif ($profit >= 116001 && $profit <= 117000) {echo "Estimated Tax = &pound; $profit19calc ";}

elseif ($profit >= 117001 && $profit <= 118000) {echo "Estimated Tax = &pound; $profit20calc ";}

elseif ($profit >= 118001 && $profit <= 119000) {echo "Estimated Tax = &pound; $profit21calc ";}

elseif ($profit >= 119001 && $profit <= 120000) {echo "Estimated Tax = &pound; $profit22calc ";}

elseif ($profit >= 120001 && $profit <= 121000) {echo "Estimated Tax = &pound; $profit23calc ";}

elseif ($profit >= 121001 && $profit <= 122000) {echo "Estimated Tax = &pound; $profit24calc ";}

elseif ($profit >= 122001 && $profit <= 123000) {echo "Estimated Tax = &pound; $profit25calc ";}

elseif ($profit >= 123001 && $profit <= 124000) {echo "Estimated Tax = &pound; $profit26calc ";}

elseif ($profit >= 124001 && $profit <= 125000) {echo "Estimated Tax = &pound; $profit27calc ";}

elseif ($profit >= 125001 && $profit <= 150000) {echo "Estimated Tax = &pound; $profit28calc ";}

elseif ($profit >= 150001) {echo "Estimated Tax = &pound; $profit29calc ";}

echo "<br><br>";

if ($profit < 6475){echo "Class 2 National Insurance = &pound; 0";}
else {echo "Class 2 National Insurance = &pound; 159.00";}

echo "<br><br>";

if ($profit < 9500){echo "Class 4 National Insurance = &pound; 0";}

if ($profit >= 9501 && $profit <= 50000){echo "Class 4 National Insurance Low Rate = &pound; $nilowrate";}
 
if ($profit >= 50001){echo "Class 4 National Insurance High Rate = &pound; $nihighrate";}

?>

Thanks

This would definitely benefit with a switch statement and maybe an array. It would simplify things and might solve your problem?

Some of the Class 4 stuff has some logic issues. I think I know what you were going for, but I didn’t want to guess and confuse you. The below is a simplification of your code. Hopefully you can adjust it to meet your needs.

Remember whenever you have code that you seem to be repeating or incrementally changing, chances are there is a better way to write it.

<?php


if( !isset( $_POST['profit']) ){
	die( 'No Profit Provided');
}

$profit = (int) $_POST['profit'];


$class2_amt = ($profit < 6475)? 0 : 159;

$class4_amt = 0;
$class4_msg = '';
if( $profit > 9500 && $profit <= 50000) { // Low
	$class4_amt = (int)((($profit - 9500) / 100) * 9);
	$class4_msg = ' Low Rate';
}else{ // High
	$class4_amt = (int)((($profit - 50000) / 100) * 2);
	$class4_msg = ' High Rate';
}

$estimatedTax = 0; // Although this should be set below, best practices is to set it anyways.

if( $profit < 12500) {
	$estimatedTax = 0;
}else if ( $profit < 50000){
	$estimatedTax = (( $profit - 12500 ) / 100) * 20;
}else if ( $profit >= 50000 && $profit < 100000){
	$estimatedTax = ((($profit - 50000) / 100) * 40) + 7500;
}else if( $profit > 150000){
	$estimatedTax = ((($profit - 150000) / 100) * 45) + 52500;
}else{ // 100000 - 150000
	$mod_count = floor( ($profit - 100000) / 1000);
	$modifier = 49500 - ($mod_count * 500);
	$estimatedTax = (($profit - $modifier) / 100 * 40) + 7500;
}
$estimatedTax = (int) $estimatedTax;

$grandTotal = ($estimatedTax + $class2_amt + $class4_amt);

echo "Estimated Tax: &pound; {$estimatedTax}.<br>
<br>
Class 2 National Insurance = &pound; {$class2_amt}.<br>
<br>
Class 4 National Insurance{$class4_msg} = &pound; {$class4_amt}.<br>
<br>
<br>
Grand Total: &pound; {$grandTotal}.<br>
";

Cheers

Thank you so much for taking time to re write this code and explain, its exactly what i was looking for. As a complete novice i can see how it works and learn from it. I appreciate your time and effort and advice and thank you for being patient as in the past i have had negative comments but he only way to learn is from your mistakes. Much respect.

i have tried this and its perfect however the grand total is not echoing on the page ? i will have a go at this see if i can learn and fix it

Sponsor our Newsletter | Privacy Policy | Terms of Service