Hello,

I am bignner in using JQuery with PHP, I have a form that culcalate the sum by using JQuery and displayed into HTML form, then I want to take this value from the form and inserted to the database by using PHP.

Could any body help me

HTML code:

<html>
....

      <td style="border:1px solid #ccc; border-width:1px 1px 0 0;"><span id="sum5">0</span></td>

      </html>

JQuery code:

<script>
    $(document).ready(function(){

        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".txtt").each(function() {

            $(this).keyup(function(){
                calculateSum();
            });
        });

    });

    function calculateSum() {

        var sum5 = 0;
        //iterate through each textboxes and add the values
        $(".txtt").each(function() {

            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum5 += parseFloat(this.value);
            }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#sum5").html(sum5.toFixed(2));
    }
</script>

Recommended Answers

All 2 Replies

Hai;

You use a hidden field for store this total value in your form.Like

<input type="hidden" name="hdnTotal" id="hdnTotal" value="" />

And also add the below code in your calculateSum().

 $("#hdnTotal").val(sum5.toFixed(2));

In your action page you will get the total value as

  $_POST['hdnTotal'] //if you use post methode
  $_GET['hdnTotal'] // if you user get methode

I've not tested this but I don't think you need to iterate your selectors to add the keyup event handler. You should be able to use the following code:-

$(".txtt").keyup(function() {
    calculateSum();
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.