0

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>
2
  • So what's the problem? Commented Apr 22, 2015 at 12:22
  • If you're just trying to post the SUM back to the server, can't you just use an .ajax or .post method? Commented Apr 22, 2015 at 12:23

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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);
        }

Comments

0

You should split your php server side scripts out of your html/js client side pages. create a separate php page so process the data and call it through an ajax call.

change your submit button to just be a button and attach an onclick event to call a function that will sum the checkboxes and then initiate the and ajax request.

<script>
function sumChecked(){
  i = 0;
  $.each($('#pedidoForm:ckecked), function({
    i++;
  });
  $.ajax({
    url:"yourPHPpage.php",
    type:"POST",
    data:{"sumVar":i},
    success: function(data){
      alert ("Process Complete");
    }
  })
}
...
</script>
...
<form class="cmxform" id="pedidoForm">
<input type="checkbox" name="metodoenvio" class="metodoenvio" preco="20" />
<input type="checkbox" name="metodoenvio" class="metodoenvio" preco="10" />
(...)
<input type="button" name="submit" id="submit" value="submit" onClick="sumChecked()"/>
</form>

then on your php page catch the $_POST['sumVar'] variable sent through from the form and do whatever you want to server-side with that info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.