-1

But I would be interested in how I can send this result from the js as a get

<form method="GET" action="order2.php">
<input type="checkbox" name="obj" price="5" value="1" >5
<input type="checkbox" name="obj" price="15" value="2">15
<input type="checkbox" name="obj" price="20" value="3">20
<input type="submit" value="send">
<div id="test"></div>

<script type="text/javascript">
$(":input").change(function() {
var values = $('input:checked').map(function () {
return $(this).attr('price');;
}).get();
var total = 0;
$.each(values,function() {
total += parseFloat(this);
});
$("#test").text(total);
});
</script>
</form>

When I select inputs 1 and 2 so that the result is in url order2.php?price=20

Pito
  • 9
  • 2

3 Answers3

0

When a form submits, it will append all of it's inputs to the query string or post data (depending on the request type). A very simple way to add another field parameter called price with the sum is just to add a hidden input and update that dynamically with the sum.

Notice that I remove the name attribute from the checkboxes. I did this so that they wouldn't be send on the form submission.

I also replaced your forEach with a reduce() as it is more functional and you can chain it with the map()

$("#myform input").change(function() {
    var total = $('input:checked').map(function () {
        return $(this).attr('price');
    }).get().reduce(function(sum, value) {
        return sum + parseFloat(value);
    }, 0);
    $(".price").val(total);
    $(".price").html(total);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform" method="GET" action="order2.php">
    <input type="checkbox" price="5" value="1" >5
    <input type="checkbox" price="15" value="2">15
    <input type="checkbox" price="20" value="3">20
    <input type="hidden" name="price" class="price"/>
    <input type="submit" value="send">
</form>
<span class="price"></span>
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
0

$(":input").change(function() {
  var values = $('input:checked').map(function () {
    return $(this).attr('price');;
  }).get();
  
  var total = 0;
  $.each(values,function() {
    total += parseFloat(this);
  });
  
 
  $("#test").text(total);

  $('form').attr('action', 'order2.php?price=' + total);
  console.log ( $('form').attr('action') ) ;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
But I would be interested in how I can send this result from the js as a get
When I select inputs 1 and 2 so that the result is in url order2.php?price=20

<form method="GET" action="order2.php">
  <input type="checkbox" name="obj" price="5" value="1" >5
  <input type="checkbox" name="obj" price="15" value="2">15
  <input type="checkbox" name="obj" price="20" value="3">20
  <input type="submit" value="send">
  <div id="test"></div>
</form>
qiAlex
  • 4,290
  • 2
  • 19
  • 35
0

From your description that when you select option 1 and 2, you have the sum of the options as the price. I'll break this down into steps:

  1. You have to sum the selected options

  2. Construct your query param

  3. Use Ajax to make your get request

This guides through how to do Ajax tutorial for post and get. Hope this helps...

ibonly
  • 133
  • 1
  • 3
  • 10