'Need a total calculation based on multiple input values

I am trying to put the List Price at the bottom of the form so the user knows how much they will pay to list their item.

I have a php file that contains the price brackets, so if the listing price is £150.00 it would be between £100 and £199.99 so that listing fee needs to be presented. If the user selects to add 'make an offer' that would incur a additional £0.50 fee.

The php is as below named (get_fee.php);

    $type=$_POST["type"];
    $auction_price=$_POST["auction_price"];
    $offer=$_POST["offer"];
 
$total_value = array(); 
   
   if($type=="Auction"){
    if($auction_price>="0.00" && $auction_price <="99.99"){
        $total_value[] = "1.00";
    }
    elseif($auction_price>="100.00" && $auction_price <="199.99"){
        $total_value[] = "2.00";
    }}
if($type=="BIN"){
    if($auction_price>="0.00" && $auction_price <="99.99"){
        $total_value[] = "1.00";
    }
    elseif($auction_price>="100.00" && $auction_price <="199.99"){
        $total_value[] = "2.00";
    }
if($offer=="Yes"){
    $total_value[] = "0.50";
}
elseif($offer=="No"){
$total_value[] = "0.00";
}



$total = array_sum($total_value);


echo' <h3>Total Listing Fee: '.$total.' ';

The form that receives the details has the relevant following inputs.

<form method="post" action="adm/new_listing.php" id="submitRequest">

<select id="type" id="type" name="type" class="form-control" onchange="yesnoCheck(this);">
                          <option value="" selected disabled>Please Select</option>
                          <option value="Auction">Auction</option>
                          <option value="BIN">Buy it Now</option>
                          <option value="Auction, BIN">Auction with Buy it Now</option>
                          <option value="Advert">Advertisement</option>
                        </select>

<input type="number" id="auction_price" name="auction_price" min="0.00" step="0.01" class="form-control" placeholder="Enter Auction Start Price">

<select class="form-control" id="offer" name="offer">
<option value="" selected disabled>Please Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>


                        </select>

<input type="text" name="total_fee" id="total_fee">

I have never done any javascript / ajax so tried to use examples from here / the web. I basically want it so that if the inputs change, it calls the php page to show the listing fee total. So I could try and do it step by step, I did it with just offer - basically if selected as yes, it would show listing fee as £0.50.

I do not get any results.

the script is;

<script>
    $("#submitRequest input").change(function(){
    var data = {
        offer : $("#offer").val(),
        ...
    }

    $.ajax({
        url: 'get_fee.php',
        type: 'POST',
        data: {
                    fee_total: fee_total
                },
                cache: false,
                success: function(dataResult){
                    $("#total_fee").html(dataResult);
                }
    });

});
</script>

Any help would be very much appreciated, so that the form calculates the listing fee. I would like for the fee to be displayed on the form, prior to submitting it / going to taking payment page.



Solution 1:[1]

you can by using array like this

$type=$_POST["type"];
$auction_price=$_POST["auction_price"];
$offer=$_POST["offer"];


$total_value = array();

//Auction Fees
if($type=="Auction"){
    if($auction_price>="0.00" && $auction_price <="99.99"){
        $total_value[] = "1.00";
    }
    elseif($auction_price>="100.00" && $auction_price <="199.99"){
        $total_value[] = "2.00";
    }
if($type=="BIN"){
    if($auction_price>="0.00" && $auction_price <="99.99"){
        $total_value[] = "1.00";
    }
    elseif($auction_price>="100.00" && $auction_price <="199.99"){
        $total_value[] = "2.00";
    }
if($offer=="Yes"){
    $total_value[] = "0.50";
}
elseif($offer=="No"){
$total_value[] = "0.00";
}


$total = array_sum($total_value);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ramez Nosshey