Umar Umar - 25 days ago 6
HTML Question

How to make a loan calculator using PHP and HTML

I want to make a loan calculator which first check eligibility of the user and then proceed next to calculate monthly installment i am using PHP and HTML for this purpose now i want to know when user click HTML button a php code or function run on clicking that button below is my code please tell me how can i take this

<?php
function checkEligibility(){
if(isset($_post['country'])=="Pakistan"){
echo "You are Eligibile for this loan";
}else{
echo "Your are Not Eligibile For This Loan";
}
return false;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Car Loan in PHP</title>
</head>
<body>
<form name="check" method="post">
<table border="1px" align="center" width="700" cellpadding="10" cellspacing="0">
<tr>
<td colspan="2" >
<div align="center" style="font-size:30px; background-color:red">This is Car Loan in PHP</div>
</td>
</tr>
<tr>
<td>Please Select Your Country :</td>
<td>
<select name="country" required>
<option value="">-- Select Your Country--</option>
<option value="Pakistan">Pakistan</option>
<option value="Usa">Usa</option>
<option value="Uk">Uk</option>
</select>
</td>
</tr>
<tr>
<td>Select Your Occupation :</td>
<td>
<select name="occupation" required>
<option value="">-- Select Your Occupation</option>
<option value="Salaried">Salaried</option>
<option value="Business">Business</option>
</select>
</td>
</tr>
<tr>
<td>
Select Your Age :
</td>
<td>
<input type="number" name="textAge" min="21" max="80"
placeholder="21" required>
</td>
</tr>
<tr>
<td>
Enter Your Monthly Income :
</td>
<td>
<input type="number" name="textIncome" min="10000"
max="8000000000" placeholder="10000" step="10000"
required>
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<button style="background-color:green; color:white;font-size:20px; border:4px solid red" onclick="return checkEligibility()" type="button" name="checkEl" value="btn1">Check Eligibility</button>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>

Answer Source

Firstly, you are missing the action attribute in your form tag.

<form name = "check" method = "post" action="where_you_want_to_do_PHP_procesing.php">

Without the action attribute, your data is not going anywhere and thus you are not able to do anything.

If you want to send the data to the source page itself (I am assuming so since you are using PHP in the same file) put

htmlentities($_SERVER['PHP_SELF'])

into the action attribute.

Secondly, if you want to access chosen select options then use the following PHP code as part of where_you_want_to_do_PHP_processing.php

<?php if(isset($_POST['checkEl'])){ $selected_val = $_POST['country']; // Storing Selected Value In Variable echo "You have selected :" .$selected_val; // Displaying Selected Value } ?>

Thirdly, in this part of your code:

<button style="background-color:green; color:white; font-size:20px; border:4px solid red" onclick="return checkEligibility()" type="button" name="checkEl" value="btn1">Check Eligibility</button>

You are calling an undefined function on onclick event. PHP does not work like javascript, PHP is a server side language and you must post or send the data somewhere in order to process it, whereas javascript processes data on the client side. Please remove it to avoid undefined behaviour.