Question: I'm using jquery to sum values of input checkbox and i need to save the sum into DB MySQL but how can i put the value in a php var? I don't know how can i do this. 

Can someone help me out? I'm newbie in jquery :/

Here's the code i'm using:

```
<script type="text/javascript">
$(document).ready(function () {     
        function recalculate() {
            var sum = 0;

            $("input[type=checkbox]:checked").each(function() {
                var val = $(this).attr("preco").replace(',', '.'); 
                sum += parseFloat(val);
            });

            $("#output").html(sum);
        }

        $("input[type=checkbox]").change(function() {
            recalculate();
        });
});
</script>

<?php
if (isset($_POST['submit'])){
$transporte = $_POST['metodoenvio'];

 (... save into DB)
}
?>

<span id="output"></span> // the sum in html shows up here

<form class="cmxform" id="pedidoForm" method="post" action="">
<input type="checkbox" name="metodoenvio" class="metodoenvio" preco="20" />
<input type="checkbox" name="metodoenvio" class="metodoenvio" preco="10" />
(...)
<input type="submit" name="submit" id="submit" value="submit"/>
</form>

```

Answer: 
Take a hidden type variable with some id in form tag and put value in `hidden variable` by `jquery` like:

```
$("#hidden_var").val(sum);

```

Then at the end submit the form
add new hidden input field to the form to hold the sum

```
    <form class="cmxform" id="pedidoForm" method="post" action="">
//add new hidden input field to have the sum
<input id="sum_input" name="sum" type="hidden"/>
    <input type="checkbox" name="metodoenvio" class="metodoenvio" preco="20" />
    <input type="checkbox" name="metodoenvio" class="metodoenvio" preco="10" />
    (...)
    <input type="submit" name="submit" id="submit" value="submit"/>
    </form>

```

//Then use the jquery to put the sum to input id sum

```
function recalculate() {
            var sum = 0;

            $("input[type=checkbox]:checked").each(function() {
                var val = $(this).attr("preco").replace(',', '.'); 
                sum += parseFloat(val);
            });

            $("#output").html(sum);
           //jquery to put sum into form
            $("#sum_input").val(sum);
        }

```