-1

I have problem with this script. I don't know how can I save variable from script to php in the same script. document.write(x) doesn't work.

I need variable "total" and "val2" from script save to php variable and use in calculation "result".

<script>
function updateDue() {
    $('#myTable tr').each(function(){
        var total = parseFloat($(this).find('.num2').val());
        var val2 = parseFloat($(this).find('.num1').val());
        // to make sure that they are numbers
        if (!total) { 
            total = 0; 
        }
        if (!val2) { 
            val2 = 0; 
        }
        var ansD = $(this).find(".remainingval");
        ansD.val(<?php
            $total= "..";
            $val2="..";
            $result=((strtotime($total) -    strtotime($val2))/3600);
            echo ($result);
        ?>);
    });
}
</script>

Do you have some idea ? Thank you very much.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
popx99
  • 7
  • 4

1 Answers1

0

You can not use javascript valiable in php like you are doing. You have to send this variable's value on server then only you can use it in server side scripting language. If you want to use strtotime funcion there then you can try getTime function in js.

var d=new Date("October 13, 1965 09:14:00");
var d2=new Date("October 20, 1995 09:14:00");
document.write(d2.getTime() - d.getTime() ); //947289600000

$d = strtotime("October 13, 1965 09:14:00");
$d2 = strtotime("October 20, 1995 09:14:00");
echo $d2-$d; //947289600

here d.getTime() is equivalent of php's strtotime().

Vaishnavesh
  • 154
  • 1
  • 2
  • 13