0

I am tring to work out a price with VAT included based on a VAT rate the user enters. I want to display the result in a input box. Can I display the result of this as the user enters the VAT rate and price without clicking a button or resubmitting the form.

I am using this to work out the price with VAT.

<?php
    $taxprice = ($tax * $selling)
?>

$tax is the value of the input box where the user enters the tax rate.
$selling is the value of the input box where the user enters the price.
$taxprice is the value I want displayed in an input box as the user enters the values for the other two fields.

I cant get the sum to work live as the user enters the two values without using a button. Does anybody know how to do this? Thanks Jon :)

3
Contributors
5
Replies
20
Views
2 Years
Discussion Span
Last Post by diafol
0

This will need to be javascript. You could do an ajaxed php function, but seems pointless. The vat field needs to know if the entry is finished before firing off the calculate function, so as a rule you can use "blur" to make this happen.
Have a go yourself with vanilla js or jQuery - whatever you're using and come back.

0

Yes, I though I would need to use some Javascript, I will have a play and see what I get. Cheers!

0

I have had a go with js. Heres my code -

<script>
var taxInput = document.getElementById('taxValue');
var sellingPriceInput = document.getElementById('sellingPrice');
var taxedPriceInput = document.getElementById('taxedPrice');

taxInput.addEventListener('keyup', calcTaxPrice);
sellingPriceInput.addEventListener('keyup', calcTaxPrice);

function calcTaxPrice() {
    var tax = taxInput.value || 0;
    var sellingPrice = sellingPriceInput.value || 0;

    var taxPrice = sellingPrice * tax;

    taxedPriceInput.value = taxprice;
    }
</script>


<input type="integer" class="form-control" name="TAXPRICE" value="<?php echo $taxprice ?>">

I was hoping this would produce the anwser in my input box however my input box remains blank after I have entered the price and and tax rate. Not really sure where I have gone wrong, if can see where I'd really appreciate your help.

0

You really don't need javascript to do this! Assuming you have the tax rate and selling price (either GET or POST variables), then you can easily do this in the server-side PHP logic. Then, output to the client browser with HTML is trivial.

0

You really don't need javascript to do this! Assuming you have the tax rate and selling price (either GET or POST variables), then you can easily do this in the server-side PHP logic. Then, output to the client browser with HTML is trivial.

But:

Can I display the result of this as the user enters the VAT rate and price without clicking a button or resubmitting the form.
...
I cant get the sum to work live as the user enters the two values without using a button

What's this for:

<input type="integer" class="form-control" name="TAXPRICE" value="<?php echo $taxprice ?>">

Is there an integer type? New on me. Show your form as I'm not convinced you've got it set up right.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.