I am trying to set up a money transfer page.
When you pick a country, It is suppose to post the rate and percentage from the database(which is working fine).
What I am trying to achieve is; when a client inputs a figure, it is suppose to multiply or divide (which is dependent on the side at which the input was made), and other few calculations as shown in the JavaScript section of the codes. Initially it was working fine until I added the first script to the file.
The first script is function that post the rate and percentage when you choose the country.
Now I am trying to set these variables in the first script to add the the second to make the calculations work.
hence;
#percent is to replace #strike
#currencyrate is to replace echo naijasend
<script>
var data = <?= @json_encode($data); ?>;
console.log(data);
function rating(id){
for(var i =0; i < data.length; i++){
if(id == data[i].id){
$("#type").val(data[i].type);
$("#currency_rate").val(data[i].currency_rate);
$("#percent").val(data[i].percent);
}
}
}
</script>
<script>
var find_net = function(gross_val,net_val){
const net_total = parseFloat(net_val) + parseFloat(gross_val);
document.getElementById("netamount").value = net_total;
}
$("#currency_value").change(function(){
var val = $(this).val();
$("#grossvalue").val((val *<?php echo $naijasend; ?>).toFixed(2));
var cedi_value = $("#grossvalue").val();
$("#strike").val((cedi_value *<?php echo $point_five; ?>).toFixed(2));
find_net($("#strike").val(), cedi_value);
});
$("#grossvalue").change(function(){
var val = $(this).val();
$("#strike").val((val *<?php echo $point_five; ?>).toFixed(2));
var cedi_value = $("#grossvalue").val();
$("#currency_value").val((val /<?php echo $naijasend; ?>).toFixed(2));
find_net($("#strike").val(), cedi_value);
});
</script>
And here is how the from looks like in html
Kindly note i am only showing the html section in question
<h4 class="text-center">Beneficiary's Details</h4>
<form method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<div class="form-group mb-4 mt-4">
<label for="">Select Country</label>
<div class="input-group">
<select name="currency_name" class='selectpicker' onchange="rating(this.value)" data-width='100%'>
<option>Select</option>
<?php
$sql = $conn->query("SELECT * FROM `exchange_rate` where `type` = 'send'");
while($ex = $sql->fetch(PDO::FETCH_ASSOC)){
$data[] = array(
'id'=>$ex['id'],
'currency_rate'=>$ex['currency_rate'],
'type'=>$ex['type'],
'percent'=>$ex['percent']
);
?>
<option value="<?= $ex['id'] ?>"><?= ucwords($ex['currency_name']) ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group mb-4 mt-4">
<label for="">Bank Name</label>
<div class="input-group ">
<input type="text" class="form-control" name="type" id="type" placeholder="Bank Name" aria-label="notification" aria-describedby="basic-addon1">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group mb-4 mt-4">
<label for="">Account Name</label>
<div class="input-group ">
<input type="text" class="form-control" name="acct_name" placeholder="Beneficiary Account Name" aria-label="notification" aria-describedby="basic-addon1" value="<?= $_POST['acct_name']?>" required>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group mb-4 mt-4">
<label for="">Account Number</label>
<div class="input-group ">
<input type="number" class="form-control" name="acct_number" placeholder="Beneficiary Account Name" aria-label="notification" aria-describedby="basic-addon1" value="<?= $_POST['acct_number']?>" required>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group mb-4 mt-4">
<label for="">Amount In Naira | CFA franc)</label>
<div class="input-group ">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><svg
xmlns="http://www.w3.org/2000/svg" width="24"
height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round"
class="feather feather-dollar-sign"><line x1="12" y1="1"x2="12"y2="23"></line><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg></span>
</div>
<input type="number" class="form-control" id="currency_value" name="currency_value" value="<?= $_POST['$currency_value']?>" placeholder="Curreny Value" step="0.01" aria-label="notification" aria-describedby="basic-addon1" required>
</div>
</div>
</div>
<div class="col-md-3">
<div class="form-group mb-4 mt-4">
<label for="">Rate</label>
<div class="input-group ">
<input type="text" class="form-control" name="currency_rate" id="currency_rate" placeholder="Rate" aria-label="notification" aria-describedby="basic-addon1" readonly>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group mb-4 mt-4">
<label for="">Amount In Cedis</label>
<div class="input-group ">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><svg
xmlns="http://www.w3.org/2000/svg" width="24"
height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round"
class="feather feather-dollar-sign"><line x1="12" y1="1"x2="12"y2="23"></line><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg></span>
</div>
<input type="number" class="form-control" id="grossvalue" name="gross_value" value="<?= $_POST['gross_value']?>" placeholder="Amount in Cedis" step="0.01" aria-label="notification" aria-describedby="basic-addon1" required>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group mb-4 mt-4">
<label for="">Charges - 0.5%</label>
<div class="input-group ">
<input type="number" class="form-control" id="percent" name="trans_fees" aria-label="notification" value="<?= $_POST['trans_fees']?>" aria-describedby="basic-addon1" readonly required>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group mb-4 mt-4">
<label for="">Total Amount</label>
<div class="input-group ">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><svg
xmlns="http://www.w3.org/2000/svg" width="24"
height="24" viewBox="0 0 24 24" fill="none"
stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round"
class="feather feather-dollar-sign"><line x1="12" y1="1"x2="12"y2="23"></line><path d="M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg></span>
</div>
<input type="number" class="form-control" id="netamount" name="amount" value="<?= $_POST['amount']?>" placeholder="Amount" aria-label="notification" aria-describedby="basic-addon1" readonly required>
</div>
</div>
</div>
I dont know what i am doing wrong. An assistance will be appreciated
An image of how the form looks like
so per the form when yu select country it shows your rate and percentage fee going to be charges
but when a value is placed in either the cedi or naira section of the form the calculations should be done(division and multiplication respectively) and strike a percentage and show as total amount.
I have the script testing on a subdomain. I have give details to provide better understaing