2014-10-30 161 views
0

對不起。 我試圖讓這個過程不起作用。 我有這樣的代碼:使用Ajax顯示php的百分比

function paypalFees($sub_total, $round_fee) { 

// Set Fee Rate Variables 
$fee_percent = '8'; // Paypal's percentage rate per transaction (3.4% in UK) 
$fee_cash = '0.00'; // Paypal's set cash amount per transaction (£0.20 in UK) 

// Calculate Fees 
$paypal_fee = ((($sub_total/100) * $fee_percent) + $fee_cash); 

if ($round_fee == true) { 
    $paypal_fee = ceil($paypal_fee); 
} 

// Calculate Grand Total 
$grand_total = ($sub_total + $paypal_fee); 

// Tidy Up Numbers 
$sub_total = number_format($sub_total, 2, '.', ','); 
$paypal_fee = number_format($paypal_fee, 2, '.', ','); 
$grand_total = number_format($grand_total, 2, '.', ','); 

// Return Array 
return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total); 

看到的結果我已經把建立一個數字:

$receipt = paypalFees('150.00', false); 

結果由

<h2>Your Receipt</h2> 
<li>Sub-Total: &pound;<?php print $receipt['sub_total']; ?></li> 
<li>Booking Fee: &pound;<?php print $receipt['paypal_fee']; ?></li> 
<li>TOTAL: &pound;<?php print $receipt['grand_total']; ?></li> 

所示看到的結果我有刷新頁面。

如何在沒有刷新頁面的情況下使用ajax來執行此過程? 一個例子: Sum HTML Textbox Values using jQuery/JavaScript

+1

查看網絡套接字,例如http://pusher.com – Utkanos 2014-10-30 13:12:45

+0

您既可以使用AJAX,也可以使用Javascript進行所有計算。 – Barmar 2014-10-30 13:13:25

回答

0

不知道你的應用程序的工作流程任何這裏是我最好的猜測:

  1. 把你的代碼出來的功能。
  2. 與普通的HTML元素讓你的主顯示頁面中的PHP頁面
  3. 使用代碼的提供和回聲出值

如果你只是簡單的顯示結果爲onload,那麼你可以做到這一點我的方式。如果你需要與用戶和數據庫的交互,你需要AJAX。

0

如果要在不刷新頁面的情況下顯示數據,則必須使用JavaScript編寫paypalFees()函數。而不是返回一個數組,你只需使用JavaScript來設置跨度的內容,如下所示:

<h2>Your Receipt</h2> 
<li>Sub-Total: &pound;<span id="receipt_subtotal"></span></li> 
<li>Booking Fee: &pound;<span id="paypal_fee"></span></li> 
<li>TOTAL: &pound;<span id="receipt_total"></span></li> 

然後讓JavaScript設置跨度的內容。

0

你可以讓這樣的事情:

<?php // getpourcent.php 
function paypalFees($sub_total, $round_fee) { 

    // Set Fee Rate Variables 
    $fee_percent = '8'; // Paypal's percentage rate per transaction (3.4% in UK) 
    $fee_cash = '0.00'; // Paypal's set cash amount per transaction (£0.20 in UK) 

    // Calculate Fees 
    $paypal_fee = ((($sub_total/100) * $fee_percent) + $fee_cash); 

    if ($round_fee == true) { 
     $paypal_fee = ceil($paypal_fee); 
    } 

    // Calculate Grand Total 
    $grand_total = ($sub_total + $paypal_fee); 

    // Tidy Up Numbers 
    $sub_total = number_format($sub_total, 2, '.', ','); 
    $paypal_fee = number_format($paypal_fee, 2, '.', ','); 
    $grand_total = number_format($grand_total, 2, '.', ','); 

    // Return Array 
    return array('grand_total'=>$grand_total, 'paypal_fee'=>$paypal_fee, 'sub_total'=>$sub_total); 
} 

exit(json_encode(paypalFees($_POST['sub_total'], $_POST['round_fee']))); 
?> 

你工作,但是有json_encode輸出。

和另一一個文件(客戶端)與一個setInterval的一個jQuery getson電話:下面Javascript對象

<h2>Your Receipt From @Ramsay Smith (thx)</h2> 
<li>Sub-Total: &pound;<span id="receipt_subtotal"></span></li> 
<li>Booking Fee: &pound;<span id="paypal_fee"></span></li> 
<li>TOTAL: &pound;<span id="receipt_total"></span></li> 
<script> 
function getPourcent() 
{ 
    $.getJSON('getpourcent.php', { 'sub_total': 0, 'round_fee': 0 }, function (data) 
    { 
    // take a look to the @Ramsay Smith code 
    $('#receipt_subtotal').text(data.sub_total); 
    $('#paypal_fee').text(data.paypal_fee); 
    $('#receipt_total').text(data.grand_total); 
    }); 
} 

setInterval(getPourcent, 500); 
</script> 
0

使用來實現這一目標,根據您的需要

var generateReceipt = { 
 

 
    paypalFees: function(round_fee) { 
 
    var sub_total = $('#tb').val(); 
 
    
 
    var fee_percent = '8'; // Paypal's percentage rate per transaction (3.4% in UK) 
 
    var fee_cash = '0.00'; // Paypal's set cash amount per transaction (£0.20 in UK) 
 
    // Calculate Fees 
 
    var paypal_fee = (((sub_total/100) * fee_percent) + fee_cash); 
 

 
    if (round_fee == true) { 
 
     paypal_fee = ceil(paypal_fee); 
 
    } 
 
    
 

 
    // Calculate Grand Total 
 
    var grand_total = (sub_total + paypal_fee); 
 

 
    // Tidy Up Numbers 
 
    sub_total = generateReceipt.number_format(sub_total, 2, '.', ','); 
 
    
 
    paypal_fee = generateReceipt.number_format(paypal_fee, 2, '.', ','); 
 
    
 
    grand_total = generateReceipt.number_format(grand_total, 2, '.', ','); 
 

 
    // Return Array 
 
    return alert('Grand_Total:' + grand_total + 'Paypal_Fee:' + paypal_fee + 'Sub_Total:' + sub_total); 
 

 
    }, 
 

 
    number_format: function(number, decimals, dec_point, thousands_sep) { 
 
    // Strip all characters but numerical ones. 
 
    number = (number + '').replace(/[^0-9+\-Ee.]/g, ''); 
 
    var n = !isFinite(+number) ? 0 : +number, 
 
     prec = !isFinite(+decimals) ? 0 : Math.abs(decimals), 
 
     sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, 
 
     dec = (typeof dec_point === 'undefined') ? '.' : dec_point, 
 
     s = '', 
 
     toFixedFix = function(n, prec) { 
 
     var k = Math.pow(10, prec); 
 
     return '' + Math.round(n * k)/k; 
 
     }; 
 
    // Fix for IE parseFloat(0.55).toFixed(0) = 0; 
 
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.'); 
 
    if (s[0].length > 3) { 
 
     s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); 
 
    } 
 
    if ((s[1] || '').length < prec) { 
 
     s[1] = s[1] || ''; 
 
     s[1] += new Array(prec - s[1].length + 1).join('0'); 
 
    } 
 
    return s.join(dec); 
 
    } 
 

 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 

 
<input type ='text' 
 
    name = 'fee' 
 
    id = 'tb' > 
 

 
<input type='button' 
 
    name ='calculate' value='Calculate' 
 
    onclick = 'generateReceipt.paypalFees();' >
修改