send the variable from javascript to php [duplicate]

2

I have this sum1 variable in javascript and would like to send it to php how can I do this?

JS

var sum1 = 0.0;
$('.class_nao_pago').each(function()
{
    sum1 += parseFloat($(this).text());

PHP

echo $sum1
    
asked by anonymous 28.01.2016 / 13:30

1 answer

1

You will only be able to do this if you call a page via AJAX (by post or get) or else create a form in your main html that sends this variable to a PHP page.

This integration you are trying to achieve can not happen.

See this example:

var sum1 = 0.0
$('.class_nao_pago').each(function()
{
    sum1 += parseFloat($(this).text());
});

$.post('URL da página PHP', {'soma':sum1})
.done(function(data){
//callback
});

Or by GET

var sum1 = 0.0
$('.class_nao_pago').each(function()
{
    sum1 += parseFloat($(this).text());
});

$.get('URL da página PHP', {'soma':sum1})
.done(function(data){
//callback
});
    
28.01.2016 / 13:44