2013-03-30 73 views
1

嗨,這是我的第一篇文章和我第一次嘗試在JavaScript(我進入PHP)。問題是,我得到一個NaN的從我的js的結果是如下(如果我能得到的代碼啄嫁接!)楠結果來自javascript

<script type="text/javascript"> 
    function Running_Total() { 
     alert('firing'); 
     var cost1 = "'.$_SESSION['75gmcost'].'"+""; 
     var cost2 = "'.$_SESSION['100gmcost'].'"+""; 
     var cost3 = "'.$_SESSION['1kgcost'].'";+""; 
     var cost4 = "'.$_SESSION['c75gmcost'].'"+""; 
     var qty1 = "'.$_SESSION['qty75gm'].'"+""; 
     var qty2 = "'.$_SESSION['qty100gm'].'"+""; 
     var qty3 = "'.$_SESSION['qty1kg'].'"+""; 
     var qty4 = "'.$_SESSION['qtyc75gm'].'"+""; 
     var sum = 0; 
     sum += cost1 * qty1; 
     sum += cost2 * qty2; 
     sum += cost3 * qty3; 
     sum += cost4 * qty4; 
     $('#tot_cost').empty(); 
     $('#tot_cost').append(sum); 
    } 
</script> 

我試圖總結從表單輸入訂單總數,但不知道足以解決南方問題。 我想我的PHP變量必須改變,以適應js,但沒有線索怎麼辦? ##

+2

那是你生成的源?因爲它看起來像你沒有實際迴應任何$ _SESSION變量的值,你只是寫字符串$ _SESSION ...嘗試將行改爲 var cost1 ='<?php echo $ _SESSION [ 「75gmcost」]; ?>'; – TommyBs

+0

不,它是我寫的源代碼(深入潛水) - 我會嘗試建議的回顯方法... – JiminSA

+0

謝謝TommyBs!我得到了一個結果 - 0!所以我的新問題是爲什麼我的算術不工作?哼哼...... – JiminSA

回答

1

嘗試在你的代碼與此替換VAR分配:

var cost1 = {$_SESSION['75gmcost']}; 
var cost2 = {$_SESSION['100gmcost']}; 
var cost3 = {$_SESSION['1kgcost']}; 
var cost4 = {$_SESSION['c75gmcost']}; 
var qty1 = {$_SESSION['qty75gm']}; 
var qty2 = {$_SESSION['qty100gm']}; 
var qty3 = {$_SESSION['qty1kg']}; 
var qty4 = {$_SESSION['qtyc75gm']}; 

在不是解釋爲一個數字(NAN)在JavaScript中值Concatinating字符串結果。

這裏有一個工作的例子,提醒「4」

<?php 
    session_start(); 
    $_SESSION['75gmcost'] = 1; 
    $_SESSION['100gmcost'] = 1; 
    $_SESSION['1kgcost'] = 1; 
    $_SESSION['c75gmcost'] = 1; 
    $_SESSION['qty75gm'] = 1; 
    $_SESSION['qty100gm'] = 1; 
    $_SESSION['qty1kg'] = 1; 
    $_SESSION['qtyc75gm'] = 1; 

    print <<< HERE 
      <script type="text/javascript"> 
    function Running_Total() { 
      alert('firing'); 
     var cost1 = {$_SESSION['75gmcost']}; 
     var cost2 = {$_SESSION['100gmcost']}; 
     var cost3 = {$_SESSION['1kgcost']}; 
     var cost4 = {$_SESSION['c75gmcost']}; 
     var qty1 = {$_SESSION['qty75gm']}; 
     var qty2 = {$_SESSION['qty100gm']}; 
     var qty3 = {$_SESSION['qty1kg']}; 
     var qty4 = {$_SESSION['qtyc75gm']}; 
     var sum = 0; 
     sum += cost1 * qty1; 
     sum += cost2 * qty2; 
     sum += cost3 * qty3; 
     sum += cost4 * qty4; 
     alert(sum); 
    } 
    Running_Total(); 
    </script> 
    HERE; 
?> 
+0

我很確定那些'{...}'在那裏是無效的。而'$ _SESSION'會被解釋爲JS變量,這可能不存在。 –

+1

@FelixKling括號僅在PHP執行期間被解釋。一旦JS執行,括號將被替換爲實際值。 – mkruzil

+0

從什麼時候PHP做到這一點?你有參考嗎? –

0

我通常會做這樣的事情(你可以看到結果這裏:http://jorgecardoso.eu/temp/test.php):

<html> 
<head> 

<?php 
    // just to test 
session_start(); 
$_SESSION['75gmcost'] = 1; 
$_SESSION['100gmcost'] = 1; 
$_SESSION['1kgcost'] = 1; 
$_SESSION['c75gmcost'] = 1; 
$_SESSION['qty75gm'] = 1; 
$_SESSION['qty100gm'] = 1; 
$_SESSION['qty1kg'] = 1; 
$_SESSION['qtyc75gm'] = 1; 
?> 

<script type="text/javascript"> 
    function Running_Total() { 
     alert('firing'); 
     var cost1 = <?php echo $_SESSION['75gmcost'] ?>; 
     var cost2 = <?php echo $_SESSION['100gmcost'] ?>; 
     var cost3 = <?php echo $_SESSION['1kgcost'] ?>; 
     var cost4 = <?php echo $_SESSION['c75gmcost'] ?>; 
     var qty1 = <?php echo $_SESSION['qty75gm'] ?>; 
     var qty2 = <?php echo $_SESSION['qty100gm'] ?>; 
     var qty3 = <?php echo $_SESSION['qty1kg'] ?>; 
     var qty4 = <?php echo $_SESSION['qtyc75gm'] ?>; 
     var sum = 0; 
     sum += cost1 * qty1; 
     sum += cost2 * qty2; 
     sum += cost3 * qty3; 
     sum += cost4 * qty4; 
     alert(sum); 
//  $('#tot_cost').empty(); 
//  $('#tot_cost').append(sum); 
    } 
</script> 

</head> 
<body> 
<script type="text/javascript"> 
Running_Total(); 
</script> 
</body> 
</html>