Jump to content

Recommended Posts

Hi im fairly new in php, i have this code working well but i have been given further instructions and im struggling with logic. This is one of the files in my app. It is a loan application developed in php. When a client borrows less than $5500,the amount should be paid in 5 months at 15%, else any amount greater than $5500 should be paid in 12 months at 22%. When i enter an amount i expect the program to calculate this. check the code below

<?php 
extract($_POST);
if(isset($save))
{

    if($source=="" || $amount=="" || $group=="" || $payment=="" || $due=="")
    {
    $err="<font color='red'>fill all the fileds first</font>";  
    }
    else
    {
$sql=mysqli_query($conn,"select * from loan where group_id='$group'");
$r=mysqli_num_rows($sql);
        if($r!=true)
        {
        mysqli_query($conn,"insert into loan values('','$group','$source','$amount','$intereset','$payment_term','$total_paid','$emi_per_month','$payment','$due')");
        
$err="<font color='blue'>Congractulations Rising Star Member grants Loan to this Member</font>";
        }
        
        else
        {
  
    $err="<font color='red'>Loan already allotted to this Member</font>";
        
        }
    }
}
  
?>
<h2 align="center" style="color:#00FFFF;text-decoration:underline">Rising Star Ventures Loan Book</h2>
<form method="post">
    
    <div class="row">
        <div class="col-sm-4"></div>
        <div class="col-sm-4"><?php echo @$err;?></div>
    </div>
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Select Member</div>
        <div class="col-sm-5">
        <select name="group" class="form-control" required>
            <option value="">Select Member</option>
            <?php 
$q1=mysqli_query($conn,"select * from groups");
while($r1=mysqli_fetch_assoc($q1))
{
echo "<option value='".$r1['group_id']."'>".$r1['group_name']."</option>";
  
}
            ?>
        </select>
        </div>
    </div>
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Income Source</div>
        <div class="col-sm-5">
        <select name="source" class="form-control" required>
            <option value="">Select Income Source</option>
            <option>Government</option>
            <option>Private Sector</option>
        </select>
        </div>
    </div>
    
    <script>
        function loanamount()
        {
        var original=document.getElementById("original").value; 
        var interest=document.getElementById("interest").value; 
        var year=document.getElementById("payment_term").value; 
        
        var interest1=(Number(original)*Number(interest)*Number(year))/100;
        var total=Number(original)+Number(interest1);
        
        var emi=(Number(original)*Number(interest)*Number(year))/100;
        document.getElementById("total_paid").value=total;
        document.getElementById("emi_per_month").value=emi;
        
        }
    </script>
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Requested Amount(Ksh.)</div>
        <div class="col-sm-5">
        <input type="number" id="original" name="amount" class="form-control" required/></div>
    </div>
    
    
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Interest Rate(30%)</div>
        <div class="col-sm-5">
        <input type="text" name="intereset" id="interest" value="30" readonly="true" class="form-control" required/></div>
    </div>
    
  
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Payment period(Monthly)</div>
        <div class="col-sm-5">
        <select onchange="loanamount()" name="payment_term" id="payment_term" class="form-control" required>
            <option value="">No of months</option>
            <?php
                for($i=1;$i<=10;$i++)
                {
                echo "<option value='".$i."'>".$i."</option>";
                }
             ?>
        </select>
        </div>
    </div>
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Interest Per Month(Ksh.)</div>
        <div class="col-sm-5">
        <input type="text" id="emi_per_month" name="emi_per_month" class="form-control" readonly/></div>
    </div>
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Total Payable Amount(Inclusive Interest)</div>
        <div class="col-sm-5">
        <input type="text" id="total_paid" name="total_paid" class="form-control" readonly/></div>
    </div>
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Loan Approval Date</div>
        <div class="col-sm-5">
        <input type="date" name="payment" min="2016-01-01" class="form-control" required/>
    
        </div>
    </div>
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-4">Payment Due Date</div>
        <div class="col-sm-5">
        <input type="date" name="due" min="2016-01-01" class="form-control" required/>
    
        </div>
    </div>
    
    
    <div class="row" style="margin-top:10px">
        <div class="col-sm-2"></div>
        <div class="col-sm-8">
        
        
<input type="submit" value="Allow New Loan" name="save" class="btn btn-success"/>
        <input type="reset" class="btn btn-success"/>
        </div>
    </div>
</form> 

 

Edited by requinix
please use Code <> for code
Link to comment
https://forums.phpfreaks.com/topic/307334-php-loan-application-logic-challenge/
Share on other sites

Take this in the spirit it's offered, but this code is a mess and really shouldn't be used on a production server. Especially if it's dealing with loan applications and tracking. You're wide open to SQL injection, none of the output is escaped, the queries are inefficient, and the mixing of logic and display is going to continue to make maintenance a nightmare.

As to your question, given the fact that you want it to calculate when you select an option, this is a JavaScript question, not a PHP question. You'll need to check the value of the loan amount (which I assume is #original) within the loanamount() function to know which set of values to apply. Read what you wrote to describe the intended behavior - that's great pseudo-code. Now, using a simple if...else construct you should be able to convert that pseudo-code almost exactly as described to functional JavaScript. Give it a go and post what you come up with here.

  • Great Answer 1

I agree, the code is no good. In case you actually wrote it and didn't copy paste it from somewhere....

1. DO NOT use extract. You are creating "Magic" variables that appear out of nowhere just like an old feature of Php that has been long removed. I dare not say it's name.

2. Font tags are from the 90's and obsolete. Use CSS

3. Use PDO and Prepared Statements. NEVER EVER put variables in a query.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.