2017-08-02 51 views
-1

我對此腳本有問題。我不知道如何將腳本中的變量從腳本保存到php中。 document.write(x)不起作用。從腳本到腳本內部PHP代碼的變量

我需要變量「total」和「val2」從腳本保存到php變量並用於計算「結果」。

<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> 

你有什麼想法嗎?非常感謝你。

+1

您需要查看如何使用AJAX – RiggsFolly

回答

0

你不能在你的php中使用javascript valiable。你必須在服務器上發送這個變量的值,然後你只能在服務器端腳本語言中使用它。 如果你想在那裏使用strtotime funcion,那麼你可以在js中嘗試getTime函數。

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 

這裏d.getTime()相當於php的strtotime()。