Kritika Gupta
Kritika Gupta

Reputation: 41

Multiply text values and display result

i am using below three fields need to multiply the Word Count Values through PHP. ANy Help ? Code as -- 1 have to select the first dropdown from here..

<select name="sp" id="sp">
<option value="" selected>Please Select...</option>
<option value="Editing">Editing</option>
<option value="Writing">Writing</option>
</select>

Then i have a text box in which the number would be provide,like word 100 200 300, as

<input type="text" name="Word_Count" id="Word_Count" size="10">

and third field would be where the price would be calculated on behalf on word from above row (100 200 300 word), as

<input type="text" name="price" id="price" value="" readonly="" size="20">

Now if the user will select the Editing from select box, the price would be calculated as 100 x 0.02, 200 x 0.02, 300 x 0.02 like and the user will select the Writing, the price would be calculated at 100 x 0.04, 200 x 0.04, 300 x 0.04.

HOw can i make the sense to get the output more efficiently ? Any help ?

Upvotes: 0

Views: 617

Answers (2)

Bhavik Shah
Bhavik Shah

Reputation: 2291

There are many complication that you will have to handle.
1) Check that the string entered in Word_Count is number. (Never trust user input. Apply validation techniques)

2) call a javascript function on Onkeyup event of Word_Count.
<input type="text" name="Word_Count" id="Word_Count" size="10" onkeyup="return somefunction()">

I have shown you, how to call a function by mentioning it in element. You can also bing jquery function on pageload. Better check-out official documentation if you want it that way.

Html select

<select name="sp" id="sp">
<option value="" selected>Please Select...</option>
<option value="0.02">Editing</option>
<option value="0.04">Writing</option>
</select>

Changed the value of options.


javascript/jquery function

function somefunction(){
    //take value of Word_Count in one variable
    var valWC = $("#Word_Count").val()

    //take value of Word_Count in one variable
    var valDP = $("#sp").val()

    //sumd will hold the product of two numbers
    var sumd = 0;

    //apply validation technique

    //multiply both values valWC and valDP
    //sumd will have the product
    sumd=Number(valWC)*Number(valDP);

    //show it in third box
    $("#price").val(sumd);
}

Upvotes: 1

Edwin Alex
Edwin Alex

Reputation: 5108

you can do this in Jquery itself.. What you have to do is,

$("#sp").change(function() {

   // get the word count field  value;

   // split the values by using  " " character and store it into an array

   // loop the array [ inside the loop do your calculation according to the selection of the choice ]


   // display the final value in the price field..
});

There are the steps to achieve it.

Upvotes: 0

Related Questions