I need to call a function when a value is entered into a textbox

I need to call a tax on a function when input is entered into a textbox and view the result in another textbox. I am using codeigniter and mysql. I wrote a function like

<?php
function tax($amount_without_tax, $tax){
   $amount_with_tax = $amount_without_tax + ($tax*($amount_without_tax/100));
   // work out the amount of vat
   $amount_with_tax = round($amount_with_tax, 2);
   return $amount_with_tax;
}
?>

      

And I have a textbox like

<input type="number" name="product_price"/>

      

how do i call this function when i enter something in the specified textbox and display them in another textbox Can someone help me ...

+3


source to share


3 answers


This code works for me

<script type="text/javascript">
            function getOrderTotal() {
                var icost = document.myform.myprice.value;

                var itax = getSalesTax();

                var recurrency = '^[0-9]{1,5}\.[0-9]{2}$';
                var reitems = '^([1-9])([0-9]{0,2})$';

                if(!icost.match(recurrency)) {
                    alert('Please provide an item cost in 0.00 format!');
                }
                else {
                    var itotal = (icost) * itax;
                    itotal *= 100;
                    itotal = Math.ceil(itotal);
                    itotal /= 100;
                    if(itotal.toFixed) {
                        itotal = itotal.toFixed(2);
                    }

                    document.getElementById('mytotal').value = itotal;
                }
            }

            function getSalesTax() {
                var taxarray = document.myform.mytax;
                //var retax = '^[1]{1}\.[0-9]{1,4}$';
                var i;

                for(i=0; i<taxarray.length; i++) {
                    if(taxarray[i].checked) {
                        if(!taxarray[i].value) {
                            alert('Please provide a tax rate from the button list only!');
                            return 0;
                        }
                        else {
                            return parseFloat(taxarray[i].value);
                        }
                    }
                }

                return 1.0;
            }
        </script>

      

Above is my script I have used textboxes and displayed the results



<div class="control-group">
            <label for="inputError" class="control-label">Wholesale Price</label>
            <div class="controls">
             <input name="myprice" type="text" id="myprice" size="10" maxlength="10" onChange="getOrderTotal()" value="0.00" />

            </div>
          </div>
          <div class="control-group">

            <label for="inputError" class="control-label">Total amount</label>
            <div class="controls">
              <input type="text" id="mytotal" name="mytotal" value="0.00">
              <!--<span class="help-inline">Cost Price</span>-->
            </div>
<div class="control-group">
            <label for="inputError" class="control-label">Tax Details</label>
                        <input name="mytax" type="radio" value="0.145" onClick="getOrderTotal()"/>&nbsp;VAT 
                        <br />
                        <input name="mytax" type="radio" value="0.1725" onClick="getOrderTotal()"/>&nbsp;CST 
                        <br />
                        <input name="mytax" type="radio" value="1.00" onClick="getOrderTotal()" checked="checked"/>&nbsp;Other (no sales tax)
                        </br>
              <!--<span class="help-inline">Cost Price</span>-->
            </div>
          </div>

      

Thank you for your support .... :)

+1


source


I think there is an onChange function.
<input type="number" name="product_price" onChange = "tax(x,y);"/>


This can call your tax function if the textbox value has changed.



0


source


<input type="number" name="product_price" id="pro_Price"/>

      

Jquery:

$( "#pro_Price" ).change(function() {
    alert( "Handler for .change() called." );
    //Do your calculation here with this.value

});

      

0


source







All Articles