Daniel
Daniel

Reputation: 117

JQuery var to form POST for PHP

To start off, sorry if this is a duplicate, or explained already. I've read a minimum of 15 other topics that I assumed are similar to mine, yet I haven't had any success in getting it to work.

I currently have a form that is action="submit.php". The form is an order form (see Jfiddle link at bottom of post). Inside submit.php I'm making an array of all $_POST values. That works great. Now the problem:

On the forum page (where user inputs), I have the following JQuery script that calculates totals. There are 3 types of totals (all this is clear in the JFiddle). The 3rd total, called "overallTotal", takes the sum of all "grandTotal"s and as of now, puts in #overallTotal ID. I need that number to be included in the form submission (i.e., so it is accessible by $_POST).

Thanks in advance, and sorry again if this is repetitive.

JSFiddle: http://jsfiddle.net/rc694dzL/

    function oninput(e) {
        // process this row first
        var row = $(e.target).closest("tr");
        // explicitly cast to a number
        var quantity = +$(e.target).val();
        var price = +row.find(".val1").text();
        var output = row.find(".multTotal");
        var total = price * quantity;
        output.text(total);

        // now calculate total
        var subtotal = 0;
        var table = $(e.delegateTarget);
        table.find(".multTotal").each(function () {
            subtotal += (+$(this).text());
        });
        table.find(".grandTotal").text(subtotal);
        // now calculate overall total
        var overallTotal = 0;
        $(document).find(".grandTotal").each(function () {
            overallTotal += (+$(this).text());
        });
        $('#overallTotal').text(overallTotal);

Upvotes: 1

Views: 72

Answers (3)

TunaMaxx
TunaMaxx

Reputation: 1769

Add some hidden fields in the form, and then populate the values with jquery.

Edit: tweaking your Fiddle now with an example: http://jsfiddle.net/dozecnp6/1/

1) Added the hidden input in the form:

<input type="hidden" name="OverallTotal" id="overallTotalField">

2) Added a bit to your oninput() function:

$('#overallTotalField').val(overallTotal);

Upvotes: 1

Man Programmer
Man Programmer

Reputation: 5356

in this fiddle i can use type="text" you can change it to type="hidden"

check this fiddle

Upvotes: 0

webNeat
webNeat

Reputation: 2828

Add a hidden input in your form like this

<input type="hidden" name="overallTotal" id="overallTotalInput">

and set the value from javascript like this

$('#overallTotalInput').val(overallTotal);

Now when submitting the form, the value will be stored into $_POST['overallTotal']

Upvotes: 1

Related Questions